简体   繁体   English

PyInstaller .exe 文件什么都不做

[英]PyInstaller .exe file does nothing

After 3 days, I can't get a python program packaged into a .exe file. 3天后,我无法将python程序打包成.exe文件。 I've tried py2exe (which continuously missed modules), and PyInstaller.我试过 py2exe(它不断错过模块)和 PyInstaller。

Here's the complicated part.这是复杂的部分。 My program uses a lot of additional installed modules (coopr, pyomo, openpyxl, glpk, cbc, pyutilib, numpy, etc.).我的程序使用了许多额外安装的模块(coopr、pyomo、openpyxl、glpk、cbc、pyutilib、numpy 等)。 These in turn import all kinds of other things, and I can't track it down (the PyInstaller warning log lists 676 lines of missing or potentially unneeded modules.)这些反过来又导入了各种其他东西,我无法追踪它(PyInstaller 警告日志列出了 676 行丢失或可能不需要的模块。)

However, I've gotten (by adding imports of "missing" modules to my program) a .exe version which runs from double clicking or from the command line, without printing any error.但是,我得到了(通过将“缺失”模块的导入添加到我的程序中)一个 .exe 版本,它通过双击或从命令行运行,没有打印任何错误。

The problem is, the program does nothing.问题是,程序什么都不做。 I have an input file which is included in the build, which my program reads in, does some (intense) calculations, and then creates a .csv output file in the same directory.我有一个包含在构建中的输入文件,我的程序读入该文件,进行一些(密集)计算,然后在同一目录中创建一个 .csv 输出文件。 It works as a .py file.它作为 .py 文件工作。 My .exe does nothing.我的 .exe 什么也不做。

So, if you can tell me what's wrong go ahead.所以,如果你能告诉我出了什么问题,请继续。 If not, I'd like to know any helpful steps or ideas to try.如果没有,我想知道任何有用的步骤或想法可以尝试。 At this point, I've exhausted the feedback I can find from the program and documentation.在这一点上,我已经用尽了可以从程序和文档中找到的反馈。

I just solved this problem for myself.我只是为自己解决了这个问题。

Make sure you do not have a folder with the same name as the script you are trying to turn into an executable.确保您没有与您尝试转换为可执行文件的脚本同名的文件夹。

If you already have application file sample.py as well as folder sample (that contains your other .py files, say) and you want the application to retain the name sample , you can work around this problem by renaming sample.py to sample_app.py and then invoke pyinstaller with the --name option eg, pyinstaller --onefile --name sample sample_app.py The binary created by pyinstaller will be called sample .如果您已经有应用程序文件sample.py以及文件夹sample (例如包含您的其他 .py 文件)并且您希望应用程序保留名称sample ,您可以通过将sample.py重命名为sample_app.py来解决此问题sample_app.py然后使用 --name 选项调用 pyinstaller 例如, pyinstaller --onefile --name sample sample_app.py由 pyinstaller 创建的二进制文件将被称为sample

When creating the exe, make sure that the python script contains创建exe时,请确保python脚本包含

if __name__ == '__main__':
    main()

at the bottom.在底部。 Otherwise, the python exe will run but since it has nothing to run it will just end.否则,python exe 将运行,但由于它没有什么可运行的,它将结束。

I also could not make work pyinstaller properly, but a few years ago I found a solution to generate R scripts executables on windows, so I tried it with python and it worked!我也无法使 pyinstaller 正常工作,但是几年前我找到了一个在 Windows 上生成 R 脚本可执行文件的解决方案,所以我用 python 尝试了它并且它起作用了!

Here is the solution, just in 3 lines in the CMD:这是解决方案,仅在 CMD 中的 3 行中:

https://github.com/jorgeavilacartes/making-my-own-executable-python https://github.com/jorgeavilacartes/making-my-own-executable-python

You can create your own extension and assing it to the python.exe you wish (if you have many venv you can create one executable extension for each one)您可以创建自己的扩展并将其分配给您希望的 python.exe(如果您有很多 venv,您可以为每个扩展创建一个可执行扩展)

Unfortunately, I only have the solution for windows.不幸的是,我只有 Windows 的解决方案。

Hope it helps!希望能帮助到你!

PD: Be careful to do not have any .ipynb in the same folder where you will create your executable, it will not run properly, I do not know why, but it was the only problem I had. PD:小心不要在创建可执行文件的同一文件夹中包含任何.ipynb ,它无法正常运行,我不知道为什么,但这是我遇到的唯一问题。

When an exe fails to run when double-clicked in windows, the associated window also automatically closes.当在 windows 中双击 exe 无法运行时,关联的窗口也会自动关闭。 If this happens for you, open the command prompt and try to run the exe there by going to the appropriate path and typing in the filename (eg C:\\github\\program\\dist\\main.py ).如果发生这种情况,请打开命令提示符并尝试通过转到适当的路径并输入文件名(例如C:\\github\\program\\dist\\main.py )来运行该 exe。 Any errors thrown by the application will be printed in the command prompt window, without it automatically closing.应用程序抛出的任何错误都将打印在命令提示符窗口中,而不会自动关闭。

If your script accesses non-Python files, move the executable so it can find them.如果您的脚本访问非 Python 文件,请移动可执行文件以便它可以找到它们。

I was having the same problem.我遇到了同样的问题。 For me, the cause was that some of my Python files required access to non-Python files (in my case, gifs).对我来说,原因是我的一些 Python 文件需要访问非 Python 文件(在我的例子中是 gifs)。 PyInstaller didn't bundle up the resources along with the Python files and didn't stick the executable in the same directory as the main Python file, so I was getting an error when my program tried to access them. PyInstaller 没有将资源与 Python 文件捆绑在一起,也没有将可执行文件与主 Python 文件放在同一目录中,因此当我的程序尝试访问它们时出现错误。 The solution was simply to copy the resources to the location where the executable was looking for them, or vise-versa.解决方案只是将资源复制到可执行文件正在寻找它们的位置,反之亦然。

For some reason, the error messages that helped me find the problem didn't generate when I ran PyInstaller normally.出于某种原因,当我正常运行 PyInstaller 时,没有生成帮助我找到问题的错误消息。 They were only generated when using the --onefile flag, and even then, they only stuck around for less than half of a second before the prompt closed.它们仅在使用--onefile标志时生成,即便如此,它们在提示关闭前只停留了不到半秒。 I had to use ctrl + prt to capture my screen when the messages appeared.当消息出现时,我不得不使用ctrl + prt来捕获我的屏幕。

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

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