Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Zenadix Avatar asked Feb 05 '15 16:02

Zenadix


People also ask

Why is my PyInstaller exe not working?

The most common reason a PyInstaller package fails is that PyInstaller failed to bundle a required file. Such missing files fall into a few categories: Hidden or missing imports: Sometimes PyInstaller can't detect the import of a package or library, typically because it is imported dynamically.

Do you need Python to run a PyInstaller EXE?

They do not need to have Python installed at all. The output of PyInstaller is specific to the active operating system and the active version of Python. This means that to prepare a distribution for: a different OS.

What is the difference between py2exe and PyInstaller?

In PyInstaller it is easy to create one exe, By default both create a bunch of exes & dlls. In py2exe its easier to embed manifest file in exe, useful for run as administrator mode in windows vista and beyond. Pyinstaller is modular and has a feature of hooks to include files in the build that you like.


1 Answers

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.

like image 90
Zenadix Avatar answered Sep 30 '22 04:09

Zenadix