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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28349359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:08:31  来源:igfitidea点击:

PyInstaller: Single-file executable doesn't run

pythonwindowspyinstaller

提问by Zenadix

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

我正在尝试从 Python 应用程序为 Windows 创建一个单文件可执行文件,使用pyinstaller.

I downloaded the experimental Python 3 branch of pyinstallerfrom here(scroll down and you'll find the download link, the file is py3.zip). And I installed it using python setup.py install. UPDATE: I tried it with the Python 2 version too, and ran into the same problem.

pyinstaller这里下载了实验性的 Python 3 分支(向下滚动,你会找到下载链接,文件是py3.zip)。我使用python setup.py install. 更新:我也用 Python 2 版本尝试过,但遇到了同样的问题。

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

然后我创建了一个名为 的测试 python 脚本test.py,内容如下:

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.exehad 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.

命令成功,我验证了文件dist/test.exe已经生成。但是,当我尝试运行它时,我得到的只是一个空的控制台窗口。什么也没有出现,程序也永远不会终止。它永远挂在那里,直到我强行关闭它。

I get an empty console window.

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

Calling pyinstaller test.py(without the --onefileoption) works fine. So what isthe problem?

调用pyinstaller test.py(没有--onefile选项)工作正常。那么,什么问题呢?

Notice that using py2exeor cx_freezeis not an option. It has to be pyinstaller.

请注意,使用py2exeorcx_freeze不是一个选项。它必须是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.

更新:我刚刚在 Python 2 下(使用普通的 PyInstaller 版本)对其进行了测试,但遇到了同样的问题。所以,这不仅仅是 Python 3 的问题。

采纳答案by Zenadix

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!message.

我发现该程序确实运行了。但是,它在显示消息之前挂了很长时间(比如 5 分钟!Hello, World!

Actually, 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 in the system. For reasons that I still can't grasp, the UPX-packed executable took an extremely long time to self-extract and run.

实际上,该问题是由UPX(Ultimate Packer for eXectutables)引起的,该工具旨在减少可执行文件的大小。如果 PyInstaller 在系统中找到 UPX,它默认使用 UPX。由于我仍然无法理解的原因,UPX 打包的可执行文件需要很长时间才能自解压和运行。

So, simply running the command with the --noupxoption fixed the problem.

因此,只需运行带有--noupx选项的命令即可解决问题。

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

For more information, check out the GitHub issue.

有关更多信息,请查看GitHub 问题