from:http://stackoverflow.com/questions/13946650/pyinstaller-2-0-bundle-file-as-onefile
OMG! This PyInstaller really confused me for a bit. If my previous post sounds a little “ranty”, sorry about that.. Anyways, for anyone trying to include a file in a –onefile PyInstaller package this worked for me:
Include this in your .py script:
1 2 3 4 5 6 7 8 9 10 11 12 | filename = 'myfilesname.type' if hasattr(sys, '_MEIPASS'): # PyInstaller >= 1.6 chdir(sys._MEIPASS) filename = join(sys._MEIPASS, filename) elif '_MEIPASS2' in environ: # PyInstaller < 1.6 (tested on 1.5 only) chdir(environ['_MEIPASS2']) filename = join(environ['_MEIPASS2'], filename) else: chdir(dirname(sys.argv[0])) filename = join(dirname(sys.argv[0]), filename) |
credit to someone online whose name I don’t remember.. (sorry it’s late and I’m exhausted!)
Then, if you’re using PyInstaller2.0, in cmd, from the pyinstaller-2.0 dir, you can run
1 | pyinstaller.py --onefile myscriptsname.py |
That will create a myscriptsname.spec file in the pyinstaller-2.0 dir. It will also create an exe, but that won’t work. It will be updated later. Now edit that .spec, and add the following a.datas line (remember datas, not data). I included a little extra in the .spec file just for reference.
1 2 3 4 5 6 | a = Analysis(['ServerTimeTest_nograph.py'], pathex=['c:\\Python27\\pyinstaller-2.0'], hiddenimports=[], hookspath=None) a.datas += [('myfilesname.type','C:\\path\\to\\my\\file\\myfilesname.type','DATA')] pyz = PYZ(a.pure) |
Now, back in cmd, run
1 | pyinstaller.py --onefile myscriptsname.spec |
This will update your .exe in the /dist dir.
Maybe there’s a better way, or a prettier way, but this worked for me!