Python Windows-Pyinstaller 错误“无法执行脚本”当应用程序点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40716346/
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
Windows- Pyinstaller Error "failed to execute script " When App Clicked
提问by aBiologist
I am having a tough time overcoming this error, I have searched everywhere for that error message and nothing seems relevant to my situation:
我很难克服这个错误,我到处搜索该错误消息,但似乎与我的情况无关:
"failed to execute script new-app"
new-appis my python GUI program. When I run pyinstaller using this command:
new-app是我的 python GUI 程序。当我使用以下命令运行 pyinstaller 时:
pyinstaller.exe --onedir --hidden-import FileDialog --windowed --noupx new-app.py
It does work smoothly. In addition, when I execute the command line to run the gui program, it works perfectly and the GUI is generated using this command:
它工作顺利。另外,当我执行命令行运行gui程序时,完美运行,使用这个命令生成GUI:
.\dist\new-app\new-app.exe
But when I go to that file hopefully to be able to click the app to get the GUI, it gives me the error said above. Why is that?
但是当我去那个文件希望能够点击应用程序来获取 GUI 时,它给了我上面说的错误。这是为什么?
I am using python2.7 and the OS is Windows 7 Enterprise.
我使用的是 python2.7,操作系统是 Windows 7 Enterprise。
Any inputs will be appreciated and thanks a lot in advance.
任何输入将不胜感激,并在此先感谢。
回答by aBiologist
Well I guess I have found the solution for my own question, here is how I did it:
好吧,我想我已经为我自己的问题找到了解决方案,这是我的方法:
Eventhough I was being able to successfully run the program using normal python command as well as successfully run pyinstaller and be able to execute the app "new_app.exe" using the command line mentioned in the question which in both cases display the GUI with no problem at all. However, only when I click the application it won't allow to display the GUI and no error is generated.
尽管我能够使用普通的 python 命令成功运行程序以及成功运行 pyinstaller 并能够使用问题中提到的命令行执行应用程序“new_app.exe”,这两种情况下都显示 GUI 没有问题根本。但是,只有当我单击应用程序时,它才会显示 GUI 并且不会生成错误。
So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error which made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.
所以,我所做的是在 pyinstaller 命令中添加了一个额外的参数 --debug 并删除了 --windowed 参数,以便我可以看到单击应用程序时实际发生的情况,我发现有一个错误导致当我追踪它时很有意义,它基本上抱怨“some_image.jpg”没有这样的文件或目录。
The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!
当我从一开始运行脚本甚至使用命令行“./”时它抱怨并且没有抱怨的原因是因为文件图像与脚本所在的路径相同,但是当pyinstaller创建“dist”时包含应用程序产品的目录很完美,图像文件不存在,所以我基本上将它移动到可点击应用程序所在的 dist 目录!
回答by J-Mourad
Add this function at the beginning of your script :
在脚本的开头添加此函数:
import sys, os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Refer to your data files by calling the function resource_path()
, like this:
通过调用函数来引用您的数据文件resource_path()
,如下所示:
resource_path('myimage.gif')
Then use this command:
然后使用这个命令:
pyinstaller --onefile --windowed --add-data todo.ico;. script.py
For more information visit this documentation page.
有关更多信息,请访问此文档页面。
回答by JacobIRR
In case anyone doesn't get results from the other answers, I fixed a similar problem by:
如果有人没有从其他答案中得到结果,我通过以下方式解决了类似的问题:
adding
--hidden-import
flags as needed for any missing modulescleaning up the associated folders and spec files:
--hidden-import
根据需要为任何缺失的模块添加标志清理相关文件夹和规范文件:
rmdir /s /q dist
rmdir /s /q dist
rmdir /s /q build
rmdir /s /q build
del /s /q my_service.spec
del /s /q my_service.spec
- Running the commands for installation as Administrator
- 以管理员身份运行安装命令
回答by Fatih1923
I got the same error and figured out that i wrote my script using Anaconda but pyinstaller
tries to pack script on pure python. So, modules not exist in pythons library folder cause this problem.
我遇到了同样的错误,并发现我使用 Anaconda 编写了我的脚本,但pyinstaller
尝试在纯 python 上打包脚本。因此,pythons 库文件夹中不存在的模块会导致此问题。
回答by Arshad
That error is due to missing of modules in pyinstaller. You can find the missing modules by running script in executable command line, i.e., by removing '-w' from the command. Once you created the command line executable file then in command line it will show the missing modules. By finding those missing modules you can add this to your command : " --hidden-import = missingmodule "
该错误是由于 pyinstaller 中缺少模块造成的。您可以通过在可执行命令行中运行脚本来找到丢失的模块,即通过从命令中删除“-w”。创建命令行可执行文件后,它将在命令行中显示缺少的模块。通过找到那些丢失的模块,您可以将其添加到您的命令中:“ --hidden-import = missingmodule ”
I solved my problem through this.
我通过这个解决了我的问题。
回答by Shyrtle
I was getting this error for a different reason than those listed here, and could not find the solution easily, so I figured I would post here.
我收到此错误的原因与此处列出的原因不同,并且无法轻松找到解决方案,因此我想我会在这里发布。
Hopefully this is helpful to someone.
希望这对某人有帮助。
My issue was with referencing files in the program. It was not able to find the file listed, because when I was coding it I had the file I wanted to reference in the top level directory and just called
我的问题是在程序中引用文件。它无法找到列出的文件,因为当我编码它时,我在顶级目录中有我想引用的文件,只是调用
"my_file.png"
when I was calling the files.
当我调用文件时。
pyinstaller did not like this, because even when I was running it from the same folder, it was expecting a full path:
pyinstaller 不喜欢这个,因为即使我从同一个文件夹运行它,它也期待一个完整的路径:
"C:\Files\my_file.png"
Once I changed all of my paths, to the full version of their path, it fixed this issue.
一旦我将所有路径更改为路径的完整版本,它就解决了这个问题。