0

I have an app that accesses .txt files that contains user data from a subdirectory where the .py file is located. After Pyinstaller is used to compile the .py file to a .exe file the path changed to AppData\Local\Temp\_MEI185962\<subdirectory>\<user data.txt> when I print the path. My question is how do I come up with the path to where the .exe file is all the time, because I want to make my program be distributeable so I don't want to use a hardcoded path or if I move the folder around.

So for example if I have my program.exe in the directory C:/Users/<username>/Desktop/project then how do I access te subdirectory C:/Users/<username>/Desktop/project/data. I tried importing os.path and had

path.dirname(path.realpath(__file__))

to get the path, but it is still pointing to the C:\User\<username>\AppData\Local\Temp\_MEI185962\<subdirectory>\<user data.txt>.

I am new to compiling so I don't know much about pyinstaller at all. I also can't seem to find any thing on this issue either.

Alexander
  • 16,091
  • 5
  • 13
  • 29
SK99
  • 51
  • 5
  • [`sys.executable`](https://pyinstaller.org/en/stable/runtime-information.html#using-sys-executable-and-sys-argv-0) – Maurice Meyer Sep 16 '22 at 23:23
  • One popular convention is to use the users ~home directory and either create or look for a directory that matches the name of your program preceded by a period `~/.AppName/userfiles.txt` then there is no need to move the directory around if the executable ever get's relocated or if there is an updated version – Alexander Sep 16 '22 at 23:26
  • Does this answer your question? [Determining application path in a Python EXE generated by pyInstaller](https://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller) – Ahmed AEK Sep 16 '22 at 23:38

0 Answers0