How to compile all resources into one executable file?

PyInstaller does allow you to bundle all your resources into the exe, without the trickyness of turning your data files into .py files — your COLLECT object seems to be correct, the tricky step is accessing these files at runtime. PyInstaller will unpack them into a temporary directory and tell you where they are with the _MEIPASS2 variable. To get the file paths in both development and packed mode, I use this:

def resource_path(relative):
    return os.path.join(
        os.environ.get(
            "_MEIPASS2",
            os.path.abspath(".")
        ),
        relative
    )


# in development
>>> resource_path("gui.glade")
"/home/shish/src/my_app/gui.glade"

# in deployment
>>> resource_path("gui.glade")
"/tmp/_MEI34121/gui.glade"

Leave a Comment