本文介绍了调用包含 PyQt4 的 PyInstaller 制作的 .exe 时出现运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个 .py 文件,一个由 QtDesigner 和另一个基本上实现了 GUI 的功能.使用 pyinstaller,我生成了一个 .exe文件以在没有 python 和相关库的系统上使用它.

So, I have two .py files, one generated by QtDesigner and another which basically implements the functionality of the GUI. Using, pyinstaller, I generated a .exe file to use it on systems without python and the associated libraries.

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

The command: pyinstaller my_script.py runs fine without any errors.

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

The problem occurs when I run the .exe file.

错误:

Qt:检测到未经测试的 Windows 10.0 版!回溯(最近一次调用最后一次):文件site-packages\PyInstaller\loader\rthooks\pyi_rth_qt4plugins.py",第 41 行,在导入错误:没有名为PySide"的模块

在处理上述异常的过程中,又发生了一个异常:

During handling of the above exception, another exception occurred:

回溯(最近一次调用最后一次):文件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 中文件C:\python\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",第 714 行,在 load_module模块 = loader.load_module(全名)

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: PyQt4.QtCore 和 PyQt5.QtCore 模块都封装了 QObject 类[11364] 无法执行脚本 pyi_rth_qt4plugins

RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class [11364] Failed to execute script pyi_rth_qt4plugins

所以我试图找到解决方案.这些是我尝试过的解决方案:

So I tried to find a solution to this. These are the solutions I tried:

如何强制 PyQt5 用于 QObject 类?- 只需导入 PyQt,因为第一条语句不能解决问题.

How to force PyQt5 use for QObject class? - simply make the PyQt import as the first statement doesn't resolve the issue.

https://github.com/tzutalin/labelImg/issues/268- 这里建议移除PyQt4只使用 PyQt5.我的系统上确实有这两个项目,有些项目依赖于 PyQt5,有些项目依赖于 PyQt4,因此我不想删除后者.此外,必须有另一个解决方案,让我不要这样做.

https://github.com/tzutalin/labelImg/issues/268 - Here it is recommended to remove PyQt4 and use only PyQt5. I do have both of them on my system, some projects rely on PyQt5 and some on PyQt4 hence I don't want remove the latter. Also, there has to be another solution, is making me not do this.

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'

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000551170-PyQt4-and-PyQt5-collisions-in-PyCharm-2017-2-1-when-debugging-QGIS-application - This was a similar error, so I added:matplotlib.rcParams['backend'] = 'Qt4Agg'matplotlib.rcParams['backend.qt4'] = 'PyQt4'

对于我的导入,仍然无效.

to my imports, still didn't work.

注意:我正在使用:

PyCharm 2018.1(社区版)

PyCharm 2018.1 (Community Edition)

构建 #PC-181.4203.547,构建于 2018 年 3 月 26 日

Build #PC-181.4203.547, built on March 26, 2018

JRE:1.8.0_152-release-1136-b20 amd64

JRE: 1.8.0_152-release-1136-b20 amd64

JVM:JetBrains s.r.o 的 OpenJDK 64 位服务器虚拟机

JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

Windows 10 10.0

Windows 10 10.0

并且代码在 IDE 中运行良好.

and the code works fine in the IDE.

编辑:

我的导入是:

from PyQt4 import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTagg as Canvas

我没有添加任何其他与 Qt 相关的导入语句.

I am not adding any other import statements related to Qt.

编辑 - 2:

尝试 cx_Freeze 而不是 PyInstaller,这是安装文件.

Trying cx_Freeze instead of PyInstaller, here is the setup file.

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,这是不间断的.有什么方法可以破解并找到我应该明确提及的库?

I added the additional_mods after executing the script once following ImportError, which are non-stop. Any ways to hack and find which libraries I should mention explicitly?

此外,当我使用 Qt 运行我的主脚本时,我还尝试检查实际导入了哪些:

Additionally, I also tried to check which libraries are actually being imported when I run my main script with Qt using:

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.

and apparently, it is importing PyQt5 internally. As mentioned before, I have NO import statements mentioning PyQt5.

编辑 - 3

所以,我将代码更改为 pure PyQt5,将 pyinstaller 更新到最新版本 - 3.4,现在有一个新的找不到 Qt 插件的问题.它仍然以某种方式导入 PyQt4,我不知道在哪里.

So, I changed the code to pure PyQt5, updated the pyinstaller to the latest version - 3.4, and now there is a new issue where it doesn't find the Qt plugins. It is still somehow importing PyQt4 and I don't know where.

推荐答案

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

So, I finally made it to work. Not the ideal case, where I don't have to change the library to PyQt5 and make sure everything is in order with PyInstaller, but it works. So here is what I did:

  1. 已安装 Python 3.5 版 - 这是因为在我更新到最新的 PyInstaller 版本 (3.4) 并尝试在 Python 3.4 上运行后,我收到一个新错误,无法找到 Qt 插件.经过一番搜索,我发现因为我使用在 适用于 Windows 的 Python 扩展包,安装未与 sip 捆绑在一起.此外,当我尝试使用 pip 在 Python 3.4 上安装 PyQt5 时,它不会安装.

  1. Installed Python version 3.5 - This was because after I updated to the latest PyInstaller version (3.4) and tried to run on python 3.4, I was getting a new error where it was not able to find Qt plugins. After some searching, I figured that since I installed PyQt5 on Python version (3.4) using a .whl file I found on Python Extension Packages for Windows, the installation didn't come bundled with sip. Additionally, when I tried to install PyQt5 on Python 3.4 using pip, it wouldn't install.

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

Installed PyQt5 and all other libraries on the new Python version using pip. Note: This version of python doesn't have a PyQt4 installed, so it is quite possible that this was the reason. I will install PyQt4 on this python version and try making the .exe again using PyInstaller and see what happens.

所以,总而言之,PyQt5 + Pyinstaller 仅适用于 Python 版本 >= 3.5.希望能帮到其他人!

So, to summarize, PyQt5 + Pyinstaller works only for Python version >= 3.5. Hope it helps others!

这篇关于调用包含 PyQt4 的 PyInstaller 制作的 .exe 时出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 20:39