Python Forum
Issue when using open() in converted exe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue when using open() in converted exe
#1
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?
Reply
#2
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".
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converted EXE file size is too large Rajasekaran 0 1,528 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  Issue w/ "With Open" vman44 3 922 Dec-23-2022, 02:35 PM
Last Post: snippsat
  Python converted EXE not working Rajasekaran 1 1,591 Aug-22-2022, 05:56 PM
Last Post: hismail_sa
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 2,839 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  TypeError: not all arguments converted during string formatting RedSkeleton007 1 14,981 Jul-15-2018, 08:51 PM
Last Post: ichabod801
  isocalendar value converted back to start of week. cspower 1 2,545 May-30-2018, 05:13 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020