When you ask a Google teacher, in many cases
python
import os
os.path.abspath(os.path.dirname(__file__))
Or something
python
import os, sys
os.path.dirname(sys.argv[0])
It will be returned after changing to. It looks good at first glance, but ** it's not good at all. ** ** What's wrong is that if you just run it normally, it's okay, but if you run it through the ** path, it's strange. ** ** To fix this
python
import os, sys
if getattr(sys, 'frozen', False):
program_directory = os.path.dirname(os.path.abspath(sys.executable))
else:
program_directory = os.path.dirname(os.path.abspath(__file__))
I will give it as. First line: Determines if it was built with Pyinstaller or not. Second line: Runs when building with Pyinstaller. Returns the path of the EXE built by ** sys.executable **. After that, convert it to a directory path with the os module. Third line: the meaning as it is Fourth line: Executed when not building with Pyinstaller. Familiar code.
~~ I was addicted to it for about 2 hours. ~~
Determining application path in a Python EXE generated by pyInstaller - stackoverflow