简体   繁体   English

PyInstaller:单文件可执行文件不运行

[英]PyInstaller: Single-file executable doesn't run

I'm trying to create a single-file executable for Windows from a Python application, using pyinstaller .我正在尝试使用pyinstaller从 Python 应用程序为 Windows 创建一个单文件可执行文件。

I downloaded the experimental Python 3 branch of pyinstaller from here (the file was python3.zip , but the link is now dead).我从这里下载了pyinstaller的实验性 Python 3 分支(文件是python3.zip ,但链接现在已经失效)。 And I installed it using python setup.py install .我使用python setup.py install安装了它。

Then I created a test python script called test.py , with the following content:然后我创建了一个名为test.py的测试 python 脚本,内容如下:

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.命令成功,我验证了文件dist/test.exe已经生成。 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.它永远挂在那里,直到我强行关闭它。

我得到一个空的控制台窗口。

Calling pyinstaller test.py (without the --onefile option) works fine.调用pyinstaller test.py (没有--onefile选项)工作正常。 So what is the problem?那么,什么问题呢?

Notice that using py2exe or cx_freeze is not an option.请注意,使用py2execx_freeze不是一种选择。 It has to be pyinstaller .它必须是pyinstaller

UPDATE: I just tested it under Python 2 (using the normal PyInstaller version), and I ran into the same problem.更新:我刚刚在 Python 2 下(使用普通的 PyInstaller 版本)对其进行了测试,但遇到了同样的问题。 So, this is not just a Python 3 problem.所以,这不仅仅是 Python 3 的问题。

I managed to solve this issue.我设法解决了这个问题。

I found out that the program actually did run.我发现该程序确实运行了。 However, it hanged for a long time ( like 5 minutes! ) before displaying the Hello, World!但是,在显示Hello, World!之前,它挂了很长时间(比如 5 分钟!Hello, World! message.信息。

Actually, the problem was caused by UPX (Ultimate Packer for eXectutables), a tool that aims to reduce the size of executable files.实际上,该问题是由UPX (Ultimate Packer for eXectutables)引起的,该工具旨在减少可执行文件的大小。 PyInstaller uses UPX by default if it finds it in the system.如果 PyInstaller 在系统中找到 UPX,它默认使用 UPX。 For reasons that I still can't grasp, the UPX-packed executable took an extremely long time to self-extract and run.由于我仍然无法理解的原因,UPX 打包的可执行文件需要很长时间才能自解压和运行。

So, simply running the command with the --noupx option fixed the problem.因此,只需运行带有--noupx选项的命令--noupx解决问题。

pyinstaller --debug --onefile --noupx test.py

For more information, check out the GitHub issue .有关更多信息,请查看GitHub 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM