[PYTHON] Get the location of the file where the exe is placed when the exe created by PyInstaller is executed

Awesome pinpoint information It's about the mac terminal. Unverified on windows

Conclusion

I got the full path of the exe file in sys.args [0], so I used it.

Where in trouble

When executing the exe file, it seems to decompress it to the user's home directory and execute the process. I wanted to spit out the excel file to the location where the exe was executed, but the excel file is generated in the home directory regardless of the location where the exe file is executed. The python file after decompressing the exe will also be extracted to your home directory.

print(os.path.abspath(__file__)))
# /Users/username/do_something.py

Try print (sys.args)

When you make a python command or exec in the terminal, pass the executable file as an argument. Yeah, isn't it possible to use print (sys.args)? I thought, I tried it and it came out.

[/Users/username/exe file storage/dist/do_something_exe_file]

Make the location of the file the same as the execution location.

After that, if you do the standard operation, it's okay

python


file_path = os.path.join(os.path.dirname(sys.argv[0]), file_name)

Impressions

I think that it is an industry where basic knowledge and peripheral knowledge are important.

Recommended Posts

Get the location of the file where the exe is placed when the exe created by PyInstaller is executed
When a file is placed in the shared folder of Raspberry Pi, the process is executed.
I want to get the path of the directory where the running file is stored.
Temporary solution to the problem that the exe file created by PyInstaller is mistaken as a Trojan horse virus by AVAST or AVG
How to get the directory where the EXE built with Pyinstaller exists
When I run the exe file with pyinstaller, my PC crashes.
EXE the application created with PyQt5 with PyInstaller
[Python] Get the character code of the file
Download the csv file created by Google Colaboratory
Get the region where AWS Lambda is running
Get the update date of the Python memo file.
Gradually display the output of the command executed by subprocess.Popen
Around the place where the value of Errbot is stored
[Python] Get the official file path of the shortcut file (.lnk)
Get the image of "Suzu Hirose" by Google image search.
Get the SQL executed by dataset (Python OR mapper)
When you think the update of ManjaroLinux is strange
The copy method of pandas.DataFrame is deep copy by default
Create an executable file (EXE) by PyInstaller in a hybrid environment (Nimporter) of Python + Nim