Anyone successfully bundled data files into a single file with Pyinstaller? Anyone successfully bundled data files into a single file with Pyinstaller? windows windows

Anyone successfully bundled data files into a single file with Pyinstaller?


You must specify every data file you want added in your pyinstaller .spec file, or via the command line options (.spec is much easier.) Below is my .spec file,with a "datas" section:

# -*- mode: python -*-block_cipher = Nonea = Analysis(['pubdata.py'],             pathex=['.', '/interface','/recommender'],             binaries=None,             datas=[('WordNet/*.txt','WordNet'),             ('WordNet/*.json','WordNet'),             ('WordNet/pdf_parsing/*.json','pdf_parsing'),             ('WordNet/pdf_parsing/*.xml','pdf_parsing'),             ('images/*.png','images'),             ('database/all_meta/Flybase/*.txt','all_meta'),             ('database/all_meta/Uniprot/*.txt','all_meta'),             ('database/json_files/*.json','json_files'),             ('Data.db','.')],             hiddenimports=['interface','recommender'],             hookspath=[],             runtime_hooks=[],             excludes=[],             win_no_prefer_redirects=False,             win_private_assemblies=False,             cipher=block_cipher)pyz = PYZ(a.pure, a.zipped_data,             cipher=block_cipher)exe = EXE(pyz,          a.scripts,          exclude_binaries=True,          name='GUI',          debug=False,          strip=False,          upx=True,          console=True )coll = COLLECT(exe,               a.binaries,               a.zipfiles,               a.datas,               strip=False,               upx=True,               name='GUI')app = BUNDLE(coll,             name='App.app',             icon=None)

After this, if you are trying to access any data file you specified in the .spec file, in your code, you must use Pyinstaller's _MEIPASS folder to reference your file. Here is how i did so with the file named Data.db:

import sysimport os.path        if hasattr(sys, "_MEIPASS"):            datadir = os.path.join(sys._MEIPASS, 'Data.db')        else:            datadir = 'Data.db'        conn = lite.connect(datadir)

This method above replaced this statement that was on its own:

conn = lite.connect("Data.db")

This link helped me a lot when i was going through the same thing: https://irwinkwan.com/2013/04/29/python-executables-pyinstaller-and-a-48-hour-game-design-compo/

Hope this helps!