【发布时间】:2019-05-07 02:02:49
【问题描述】:
所以,我有两个 .py 文件,一个由QtDesigner 生成,另一个基本上实现了 GUI 的功能。使用pyinstaller,我生成了一个 .exe 文件,以便在没有 python 和相关库的系统上使用它。
命令:pyinstaller my_script.py 运行良好,没有任何错误。
当我运行 .exe 文件时出现问题。
错误:
Qt:检测到未经测试的 Windows 10.0 版! 回溯(最近一次通话最后): 文件“site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py”,第 41 行,在 ImportError:没有名为“PySide”的模块
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次): 文件“site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py”,第 43 行,在 _find_and_load 中的文件“”,第 2237 行 文件“”,第 2226 行,在 _find_and_load_unlocked 文件“”,第 1191 行,在 _load_unlocked _load_backward_compatible 中的文件“”,第 1161 行 文件“C:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”,第 714 行,在 load_module 模块 = loader.load_module(全名)
RuntimeError:PyQt4.QtCore 和 PyQt5.QtCore 模块都包装了 QObject 类 [11364] 执行脚本 pyi_rth_qt4plugins 失败
所以我试图找到解决方案。这些是我尝试过的解决方案:
How to force PyQt5 use for QObject class? - 只需导入 PyQt,因为第一条语句不能解决问题。
https://github.com/tzutalin/labelImg/issues/268 - 这里建议删除 PyQt4 和 仅使用 PyQt5。我的系统上确实有这两个项目,一些项目依赖于 PyQt5 而一些项目依赖于 PyQt4 因此我不想删除后者。另外,必须有另一个解决方案,这让我不要这样做。
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000551170-PyQt4-and-PyQt5-collisions-in-PyCharm-2017-2-1-when-debugging-QGIS-application - 这是一个类似的错误,所以我补充说:
matplotlib.rcParams['backend'] = 'Qt4Agg'
matplotlib.rcParams['backend.qt4'] = 'PyQt4'
到我的进口,仍然没有工作。
注意: 我正在使用:
PyCharm 2018.1(社区版)
构建 #PC-181.4203.547,构建于 2018 年 3 月 26 日
JRE:1.8.0_152-release-1136-b20 amd64
JVM:JetBrains s.r.o 的 OpenJDK 64 位服务器虚拟机
Windows 10 10.0
并且代码在 IDE 中运行良好。
编辑:
我的进口是:
from PyQt4 import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTagg as Canvas
我没有添加任何其他与 Qt 相关的导入语句。
编辑 - 2:
尝试 cx_Freeze 而不是 PyInstaller,这里是安装文件。
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
additional_mods = ['numpy.core._methods', 'numpy.lib.format',
'numpy._distributor_init']
setup( name="ASCII2fig",
version = "0.1",
description = "GUI",
options = {'build_exe': {'includes': additional_mods}},
executables = [Executable("ASCII2figALL_main_edited.py", base=base)])
我在ImportError 之后执行一次脚本后添加了additional_mods,这是不间断的。有什么方法可以破解并找到我应该明确提及的库?
此外,当我使用 Qt 运行我的主脚本时,我还尝试检查哪些 库 实际被导入:
from modulefinder import ModuleFinder
filename = "ASCII2figALL_main_edited.py"
finder = ModuleFinder()
finder.run_script(filename)
for name, mod in finder.modules.items():
print(name)
显然,它正在在内部导入 PyQt5。如前所述,我有 NO 提到 PyQt5 的导入语句。
编辑 - 3
所以,我把代码改成了pure PyQt5,把pyinstaller更新到最新版本——3.4,现在有了新的找不到 Qt 插件的问题。它仍然以某种方式导入 PyQt4,我不知道在哪里。
【问题讨论】:
-
您反对使用其他 exe 编译器吗?我在我所有的 PyQt 项目中都使用 cx_Freeze,它总是能在最少 tweek 的设置代码中运行良好。
-
是的,正如我所提到的,我正在使用 pyinstaller。我可以试试 cx_Freeze。但它仍然困扰我为什么这个问题没有得到解决。
-
为什么所有可能的 Qt-Python 绑定都会引发一些错误(PySide、PyQt4、PyQt5)?您是否一次全部使用它们?通常大多数“编译器”应该开箱即用地处理 PyQt。
-
@MauriceMeyer 不在这个文件中我没有,这里我只使用 PyQt4。我将导入添加为编辑
-
应该没那么难。它绝对不像 pyinstaller 那样简单,但是一旦你找到正确的依赖关系,它应该是快速和简单的。您介意发布您拥有的 setup.py 吗?我不确定手动添加库是什么意思,有些无法正确加载(tkinter,一些 numpy),但您可以在设置中“包含”它们。
标签: python pyqt4 pyqt5 pyinstaller