命令:
pyinstaller my_script.py
运行正常,没有任何错误。问题出现在我运行.exe文件时。
错误:
Qt:检测到未经测试的 Windows 版本 10.0! Traceback(最近一次调用最先): File“site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py”,第41行, ImportError:找不到名为'PySide'的模块 在处理上述异常期间,发生了另一个异常: Traceback(最近一次调用最先): File“site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py”,第43行, 文件“”,第2237行,在_find_and_load 文件“”,第2226行,在_find_and_load_unlocked 文件“”,第1191行,在_load_unlocked 文件“”,第1161行,在_load_backward_compatible File“C:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”,第714行,加载模块 module = loader.load_module(fullname) RuntimeError:PyQt4.QtCore 和 PyQt5.QtCore 模块都包装 QObject 类 [11364]无法执行脚本 pyi_rth_qt4plugins
所以我尝试寻找解决方案。 这些是我尝试过的解决方案:
如何强制PyQt5用于QObject类? - 简单地将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'
到我的import语句中,但还是无效。
注: 我使用的是:
PyCharm 2018.1(社区版)
Build #PC-181.4203.547,构建于2018年3月26日
JRE:1.8.0_152-release-1136-b20 amd64
JVM:由JetBrains s.r.o提供的OpenJDK 64位服务器VM
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)])
在执行脚本后,我添加了additional_mods
,以解决不断出现的ImportError
。有没有什么方法可以找到我应该明确提及哪些库来进行hack?
此外,我还尝试使用以下方式检查在使用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。如前所述,我没有任何提到PyQt5的import语句。
编辑-3
因此,我将代码更改为纯PyQt5,将pyinstaller更新到最新版本-3.4,现在出现了一个新问题,它找不到Qt插件。 它仍然以某种方式导入PyQt4,我不知道在哪里。