Sometimes, we want to determine application path in a Python EXE generated by pyInstaller.
In this article, we’ll look at how to determine application path in a Python EXE generated by pyInstaller.
How to determine application path in a Python EXE generated by pyInstaller?
To determine application path in a Python EXE generated by pyInstaller, we can check the sys.frozen property.
For instance, we write
import os
import sys
config_name = 'myapp.cfg'
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
config_path = os.path.join(application_path, config_name)
to get the value of sys.frozen with
getattr(sys, 'frozen', False)
If the value is True, then the script code is running in the exe file.
Then we get the path of the exe it’s running on with
os.path.dirname(sys.executable)
Otherwise, the script it running as a script and we get the path of the script with
os.path.dirname(__file__)
Conclusion
To determine application path in a Python EXE generated by pyInstaller, we can check the sys.frozen property.