PyQt5 使用 Pyinstaller 打包 exe 时提示 failed to execute script 如何解决?
系统环境为:win7 32 位虚拟机
python 3.5.2
pyinstaller 3.3.1
PyQt5.9
python 文件在未打包时可运行
但是-w 打包后提示: failed to execute script DKExam
打包时情况如下:
C:\Users\xxx\Documents\exam>pyinstaller DKExam.py -w
3406 INFO: PyInstaller: 3.3.1
3406 INFO: Python: 3.5.2
3407 INFO: Platform: Windows-Vista-6.0.6000-SP0
3408 INFO: wrote C:\Users\xxx\Documents\exam\DKExam.spec
3758 INFO: UPX is not available.
3771 INFO: Extending PYTHONPATH with paths
['C:\\Users\\xxx\\Documents\\exam', 'C:\\Users\\xxx\\Documents\\exam']
3771 INFO: checking Analysis
7792 INFO: checking PYZ
8021 INFO: checking PKG
8028 INFO: Building because C:\Users\xxx\Documents\exam\build\DKExam\DKExam.exe.
manifest changed
8028 INFO: Building PKG (CArchive) out00-PKG.pkg
8733 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
8735 INFO: Bootloader c:\users\xxx\appdata\local\programs\python\python35-32\lib
\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe
8736 INFO: checking EXE
8758 INFO: Building because manifest changed
8759 INFO: Building EXE from out00-EXE.toc
8883 INFO: Appending archive to EXE C:\Users\xxx\Documents\exam\build\DKExam\DKE
xam.exe
9025 INFO: Building EXE from out00-EXE.toc completed successfully.
9029 INFO: checking COLLECT
WARNING: The output directory "C:\Users\xxx\Documents\exam\dist\DKExam" and ALL
ITS CONTENTS will be REMOVED! Continue? (y/n)y
12541 INFO: Removing dir C:\Users\xxx\Documents\exam\dist\DKExam
14092 INFO: Building COLLECT out00-COLLECT.toc
44921 INFO: Building COLLECT out00-COLLECT.toc completed successfully.
在 64 位 win10 的物理机上打包可行
但是由于物理机是 64 位 Python,打包后要在32 位无 python 环境 win7 上运行测试
所以无奈只能用虚拟机,却碰到了这个问题
希望大佬们能出手相助,不胜感激
PyQt5 使用 Pyinstaller 打包 exe 时提示 failed to execute script 如何解决?
PyQt5用PyInstaller打包报failed to execute script,这问题太常见了。别慌,按下面几步排查,基本都能搞定。
第一步:先看详细报错信息
别光看最后一句failed to execute script,关键信息在前面。在命令行里运行打包好的exe,或者用--debug模式打包,就能看到具体报错。
第二步:检查依赖和路径问题
PyQt5应用经常缺动态库或资源文件。最简单的排查方法是加--hidden-import和检查资源路径:
# 你的主程序文件(比如 main.py)开头加上资源路径修复
import sys
import os
# 解决PyInstaller打包后资源路径问题
if getattr(sys, 'frozen', False):
# 如果是打包后的exe
base_path = sys._MEIPASS
else:
# 如果是开发环境
base_path = os.path.dirname(__file__)
# 设置Qt插件路径(重要!)
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = os.path.join(base_path, 'PyQt5', 'Qt', 'plugins')
# 然后正常导入PyQt5
from PyQt5 import QtWidgets
# ... 你的其他代码
第三步:正确的打包命令 用这个命令打包,基本能解决90%的问题:
pyinstaller --noconsole --onefile ^
--hidden-import PyQt5.sip ^
--hidden-import PyQt5.QtCore ^
--hidden-import PyQt5.QtGui ^
--hidden-import PyQt5.QtWidgets ^
--add-data "venv/Lib/site-packages/PyQt5/Qt/plugins/platforms;PyQt5/Qt/plugins/platforms" ^
--add-data "venv/Lib/site-packages/PyQt5/Qt/plugins/imageformats;PyQt5/Qt/plugins/imageformats" ^
main.py
第四步:如果还不行,用spec文件 创建spec文件能更精细控制打包:
# 生成spec文件
pyi-makespec --noconsole --onefile main.py
然后编辑生成的main.spec,在Analysis部分添加:
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=[
('venv/Lib/site-packages/PyQt5/Qt/plugins/platforms/*', 'PyQt5/Qt/plugins/platforms'),
('venv/Lib/site-packages/PyQt5/Qt/plugins/imageformats/*', 'PyQt5/Qt/plugins/imageformats'),
('venv/Lib/site-packages/PyQt5/Qt/bin/*', '.') # 有时需要这个
],
hiddenimports=['PyQt5.sip', 'PyQt5.QtCore', 'PyQt5.QtGui', 'PyQt5.QtWidgets'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=None,
noarchive=False
)
最后用spec文件打包:
pyinstaller main.spec
第五步:终极方案——手动收集依赖 如果上面都不行,手动把所有依赖拷到一起:
- 先正常打包一次
- 把
dist目录下的exe单独拷到新文件夹 - 从Python安装目录的
Lib/site-packages/PyQt5里把整个Qt文件夹拷到exe同级目录 - 运行测试
总结建议:先用–debug模式打包看具体报错,再针对性解决。
win10 64 位 虚拟机 py3.6.3 pyinstaller3.3.1 遇到同样错误
解决了, 有个 Python 库没有在虚拟机上安装

