I\'m trying to build a one-file EXE with PyInstaller which is to include an image and an icon. I cannot for the life of me get it to work with --onefile
.
I found the existing answers confusing, and took a long time to work out where the problem is. Here's a compilation of everything I found.
When I run my app, I get an error Failed to execute script foo
(if foo.py
is the main file). To troubleshoot this, don't run PyInstaller with --noconsole
(or edit main.spec
to change console=False
=> console=True
). With this, run the executable from a command-line, and you'll see the failure.
The first thing to check is that it's packaging up your extra files correctly. You should add tuples like ('x', 'x')
if you want the folder x
to be included.
After it crashes, don't click OK. If you're on Windows, you can use Search Everything. Look for one of your files (eg. sword.png
). You should find the temporary path where it unpacked the files (eg. C:\Users\ashes999\AppData\Local\Temp\_MEI157682\images\sword.png
). You can browse this directory and make sure it included everything. If you can't find it this way, look for something like main.exe.manifest
(Windows) or python35.dll
(if you're using Python 3.5).
If the installer includes everything, the next likely problem is file I/O: your Python code is looking in the executable's directory, instead of the temp directory, for files.
To fix that, any of the answers on this question work. Personally, I found a mixture of them all to work: change directory conditionally first thing in your main entry-point file, and everything else works as-is:
if hasattr(sys, '_MEIPASS'):
os.chdir(sys._MEIPASS)