调用使用 PyInstaller(包括 PyQt4)制作的 .exe 时出现运行时错误

Runtime error when calling a .exe made with PyInstaller including PyQt4

所以,我有两个 .py 文件,一个由 QtDesigner and another which basically implements the functionality of the GUI. Using, pyinstaller 生成,我生成了一个 .exe 文件来在没有 python 和相关库的系统上使用它。

命令:pyinstaller my_script.py 运行很好,没有任何错误。

当我 运行 .exe 文件时出现问题。

错误:

Qt: Untested Windows version 10.0 detected! Traceback (most recent call last): File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py", line 41, in ImportError: No module named 'PySide'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py", line 43, in File "", line 2237, in _find_and_load File "", line 2226, in _find_and_load_unlocked File "", line 1191, in _load_unlocked File "", line 1161, in _load_backward_compatible File "C:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 714, in load_module module = loader.load_module(fullname)

RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class [11364] Failed to execute script 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-b20amd64

JVM:JetBrains 的 OpenJDK 64 位服务器 VM s.r.o

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,我不知道在哪里。

所以,我终于成功了。这不是理想的情况,我不必将库更改为 PyQt5 并确保 PyInstaller 一切正常,但它可以工作。所以这就是我所做的:

  1. 已安装 Python 版本 3.5 - 这是因为在我更新到最新的 PyInstaller 版本 (3.4) 并尝试在 python 3.4 上 运行 之后,我在无法找到 Qt 插件的地方遇到了一个新错误。经过一番搜索,我发现由于我使用在 Python Extension Packages for Windows 上找到的 .whl 文件在 Python 版本 (3.4) 上安装了 PyQt5,因此安装并未与 sip[ 捆绑在一起=26=]。此外,当我尝试使用 pip 在 Python 3.4 上安装 PyQt5 时,它不会安装。

  2. 使用 pip 在新的 Python 版本上安装了 PyQt5 和所有其他库。 注意:这个版本的python没有安装PyQt4,所以很可能就是这个原因。我将在此 python 版本上安装 PyQt4,并尝试使用 PyInstaller 再次制作 .exe,看看会发生什么。

因此,总而言之,PyQt5 + Pyinstaller 仅适用于 Python 版本 >= 3.5。希望对大家有帮助!