Skip to content Skip to sidebar Skip to footer

Include Data With Spec File Using Pyinstaller

I am trying to bundle an application written in Python and include the relevant data files in the bundle. What is wrong with the way I am adding in data? This is using a spec file

Solution 1:

The way you are bundling the files are fine but I recommend you to use add-data flag with the build arguments. I think your problem is with the way you retrieve the files. According to docs:

When a bundled app starts up, the bootloader sets the sys.frozen attribute and stores the absolute path to the bundle folder in sys._MEIPASS. For a one-folder bundle, this is the path to that folder. For a one-file bundle, this is the path to the temporary folder created by the bootloader.

So you bundle the files PyInstaller would extract them in a temporary directory something like C:/Users/<Username>/AppData/Local/Temp/_MEIxxxxxx. You need to retrieve the files from there.

import os
import sys


def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        returnos.path.join(sys._MEIPASS, relative_path)
    returnos.path.join(os.path.abspath("."), relative_path)


if __name__ == "__main__":
    file_one_path = resource_path("file_one.pickle")
    file_two_path = resource_path("file_two.pickle")
    file_three_path = resource_path("file_three.pickle")

Post a Comment for "Include Data With Spec File Using Pyinstaller"