gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:05:34 25 4
gpt4 key购买 nike

所以,我有两个 .py 文件,一个由 QtDesigner 生成另一个基本上实现了 GUI 的功能。使用,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['后端'] = '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 位服务器 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)])

我在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。如前所述,我没有提到 PyQt5 的导入语句。

编辑 - 3

所以,我将代码更改为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 捆绑在一起。此外,当我尝试使用 pip 在 Python 3.4 上安装 PyQt5 时,它无法安装。

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

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

关于python - 调用使用 PyInstaller(包括 PyQt4)制作的 .exe 时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53633365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com