Search code examples
pythonwindowspyinstaller

PyInstaller: Single-file executable doesn't run


I'm trying to create a single-file executable for Windows from a Python application, using pyinstaller.

I downloaded the experimental Python 3 branch of pyinstaller from here (the file was python3.zip, but the link is now dead). And I installed it using python setup.py install.

Then I created a test python script called test.py, with the following content:

print('Hello, World!')

Afterwards, I ran the following command to create a single-file executable:

pyinstaller --onefile test.py

The command succeeded, and I verified that the file dist/test.exe had been generated. However, when I try to run it, all I get is an empty console window. Nothing ever appears, and the program never terminates. It just hangs there forever, until I force close it.

I get an empty console window.

Calling pyinstaller test.py (without the --onefile option) works fine. So what is the problem?

Notice that using py2exe or cx_freeze is not an option. It has to be pyinstaller.

UPDATE: I just tested it under Python 2 (using the normal PyInstaller version), and I ran into the same problem. So, this is not just a Python 3 problem.


Solution

  • I managed to solve the issue.

    I found out that the program did, in fact, run. However, it hung for a long time (like 5 minutes!) before displaying the Hello, World! message.

    The problem was caused by UPX (Ultimate Packer for eXectutables), a tool that aims to reduce the size of executable files. PyInstaller uses UPX by default if it finds it on the system. For reasons that I still can't grasp, the UPX-packed executable took an extremely long time to self-extract and run.

    Thus, simply running the command with the --noupx option fixed the problem.

    pyinstaller --onefile --noupx test.py
    

    As a sidenote, adding the --debug option to the pyinstaller command can usually help identify problems such as this one.