pyinstaller changes value of file path

Viewed 99

I am running the following code:

DIRECTORY_PATH = os.getcwd()
print(DIRECTORY_PATH)
print(__file__)
print(os.path.abspath(__file__))
input("Press enter to continue ...")

But I get different results when I run after I use pyinstaller versus running just the python script. Is there a way to get the directory that the exe is in?

Running pyinstaller --onefile exe (notice the blue powershell behind the black python shell that contains the command used to run the exe)

Running python script with python

1 Answers

The only way I found around this is to make a batch file to run the program from the same directory.

cd %~dp0
start program.exe

That way you get the directory path to the starting file even though its technically because its coming from the batch file.

Related