Skip to content

Instantly share code, notes, and snippets.

@quietcricket
Created August 24, 2023 02:27
Show Gist options
  • Save quietcricket/009590ffa40eb22855751a87d93b7740 to your computer and use it in GitHub Desktop.
Save quietcricket/009590ffa40eb22855751a87d93b7740 to your computer and use it in GitHub Desktop.
pyinstaller can package a python app into a single executable file. When this executable file runs, it's first extracted into a temporary location then the python script is called. The usual loading of files with relative path would not work. The following code is found online to work around this issue.
def abspath(filename):
# looking for magic word _MEIPASS
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
return os.path.abspath(os.path.join(bundle_dir, filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment