Python Forum
Issue when using open() in converted exe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Issue when using open() in converted exe (/thread-36249.html)



Issue when using open() in converted exe - skakalpes - Feb-01-2022

Hi. I'm trying to solve a critical issue that prevents to continue working on my project. I think it's something easy to solve and I'm probably just missing something obvious, however, I couldn't find the resolution online.

I'm using open() to read the file content.
move_f = open(vkode_location + "devview\\files\\stats\\stats_console.saver", "r")
print("You've opened Console " + move_f.read() + " times")
move_f.close()
When I run it on my device, everything works perfectly. But when I convert it to an executable, it inserts "v" to the file location for some reason.

Output:
Traceback (most recent call last): File "console.py", line 207, in <module> result, error = vkode.run('<stdin>', text) File "vkode.py", line 2848, in run result = interpreter.visit(ast.node, context) File "vkode.py", line 2527, in visit return method(node, context) File "vkode.py", line 2547, in visit_ListNode elements.append(res.register(self.visit(element_node, context))) File "vkode.py", line 2527, in visit return method(node, context) File "vkode.py", line 2749, in visit_CallNode return_value = res.register(value_to_call.execute(args)) File "vkode.py", line 1708, in execute return_value = res.register(method(exec_ctx)) File "vkode.py", line 1929, in execute_vkStats move_f = open(vkode_location + "devview\\files\\stats\\stats_console.saver", "r") FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Soubory\\jine\\Developing\\vkode\\6 - kopie (2) - kopie\\dist\\console\\vdevview\\files\\stats\\stats_console.saver' [7216] Failed to execute script 'console' due to unhandled exception!
The code should open devview\\files\\stats\\stats_console.saver, not vdevview\\files\\stats\\stats_console.saver

The pyinstaller command:
pyinstaller --noconfirm --onedir --console --icon "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/icon.ico" --no-embed-manifest --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/cache;cache/" --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/devview;devview/" --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/devview/files;devview/files/" --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/devview/files/savers;devview/files/savers/" --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/devview/files/stats;devview/files/stats/" --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/lib;lib/" --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/console.py;." --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/update.py;." --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/vkode.py;." --add-data "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/build-exe with python.py;." "D:/Soubory/jine/Developing/vkode/6 - kopie (2) - kopie/console.py"

I've tried a lot of things but nothing has solved the problem.
Can you please help me?


RE: Issue when using open() in converted exe - deanhystad - Feb-01-2022

What is the value of vkode_location? It would be interesting to know if the "v" is added there.

I don't think you are using --add-data correctly. I am more concerned that the program is trying to access "'D:\\Soubory\" than a mystery "v".


RE: Issue when using open() in converted exe - snippsat - Feb-02-2022

You can not use absolute path to file system outside of the build,
then point is that this shall work on other Pc that dos not have a D:/Soubory/ folder.
Pyinstaller most find stats_console.saver in the build together with .exe.

Example:
# f_read.py
with open('test.saver') as f:
    print(f.read())

input('Press enter to exit')
Now build like this pyinstaller --onedir --add-data "test.saver;." f_read.py
Test:
# At start files in same folder
C:\code\py_file
λ ls
f_read.py  test.saver

# Build
C:\code\py_file
λ pyinstaller --onedir --add-data "test.saver;." f_read.py
80 INFO: PyInstaller: 4.8
80 INFO: Python: 3.10.0
99 INFO: Platform: Windows-10-10.0.19043-SP0
....
7371 INFO: Building COLLECT COLLECT-00.toc completed successfully.

# Navigate to folder
# See that test.saver in build folder together with f_read.exe
C:\code\py_file
λ cd dist\f_read\
λ ls
VCRUNTIME140.dll*  _hashlib.pyd*          _socket.pyd*        libffi-7.dll*    test.saver
_asyncio.pyd*      _lzma.pyd*             _ssl.pyd*           libssl-1_1.dll*  unicodedata.pyd*
_bz2.pyd*          _multiprocessing.pyd*  base_library.zip    pyexpat.pyd*
_ctypes.pyd*       _overlapped.pyd*       f_read.exe*         python310.dll*
_decimal.pyd*      _queue.pyd*            libcrypto-1_1.dll*  select.pyd*

# Test f_read.exe
C:\code\py_file\dist\f_read
λ f_read.exe
Hello world
Press enter to exit