#๐Ÿ”’ Attempting to create single exe file that can read data files and folders from relative directory

16 messages ยท Page 1 of 1 (latest)

desert lotus
#

I have a program that reads, edits and writes to a lot of folders using a relative path. I want to convert this file to an exe and have that exe file be able to simply 'take the place' of that python file, so that the executable can be moved to different directories with the same file structure and still work well with different data. I'm trying to use pyinstaller to do this, but I'm confused about the --add-data parameter.

Inside of the docs, it says that if --one-file is chosen, then it will create a temporary directory with the data files and operate there, then delete it when the program finishes running.

distant riverBOT
#

@desert lotus

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

desert lotus
#

If I add a relative path to that parameter, will the exe use that same relative path wherever it is or is it just on the exe's creation?

#

if it's the latter, then how can I make an single exe able to work with files using relative paths?

wise pilot
#

Wait, or are you asking how to reference data files? In that case use __file__ and you'll get the path to the unpacked bundle.

desert lotus
desert lotus
#

like

#

let's say the a directory has main.py and data.txt

wise pilot
#

--add-data is for including files into the bundle while building. If you want to read some file from outside the bundle, you don't need it.

desert lotus
#

oh ok then that answers that

#

so then I should use sys.executable to locate the files at runtime?

wise pilot
#

To locate the .exe, sure

distant riverBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.