简体   繁体   English

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

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

So, I have two .py files, one generated by QtDesigner and another which basically implements the functionality of the GUI. 所以,我有两个.py文件,一个由QtDesigner生成,另一个基本上实现了GUI的功能。 Using, pyinstaller , I generated a .exe file to use it on systems without python and the associated libraries. 使用pyinstaller ,我生成了一个.exe文件,在没有python和相关库的系统上使用它。

The command: pyinstaller my_script.py runs fine without any errors. 命令: pyinstaller my_script.py运行正常,没有任何错误。

The problem occurs when I run the .exe file. 运行.exe文件时出现问题。

Error: 错误:

Qt: Untested Windows version 10.0 detected! Qt:检测到未经测试的Windows版本10.0! Traceback (most recent call last): File "site-packages\\PyInstaller\\loader\\rthooks\\pyi_rth_qt4plugins.py", line 41, in ImportError: No module named 'PySide' Traceback(最近一次调用最后一次):在ImportError中输入文件“site-packages \\ PyInstaller \\ loader \\ rthooks \\ pyi_rth_qt4plugins.py”,第41行:没有名为'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) 回溯(最近一次调用最后一次):文件“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(fullname)

RuntimeError: the PyQt4.QtCore and PyQt5.QtCore modules both wrap the QObject class [11364] Failed to execute script pyi_rth_qt4plugins RuntimeError:PyQt4.QtCore和PyQt5.QtCore模块都包装QObject类[11364]无法执行脚本pyi_rth_qt4plugins

So I tried to find a solution to this. 所以我试图找到解决方案。 These are the solutions I tried: 这些是我尝试的解决方案:

How to force PyQt5 use for QObject class? 如何强制PyQt5用于QObject类? - simply make the PyQt import as the first statement doesn't resolve the issue. - 只需进行PyQt导入,因为第一个语句无法解决问题。

https://github.com/tzutalin/labelImg/issues/268 - Here it is recommended to remove PyQt4 and use only PyQt5 . https://github.com/tzutalin/labelImg/issues/268 - 这里建议删除PyQt4仅使用 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. 我确实在我的系统上有两个,有些项目依赖于PyQt5 ,有些项目依赖于PyQt4,因此我不想删除后者。 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 - This was a similar error, so I added: 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 -这是一个类似的错误,所以我补充说: matplotlib.rcParams['backend'] = 'Qt4Agg' matplotlib.rcParams['backend.qt4'] = 'PyQt4'

to my imports, still didn't work. 我的进口,仍然没有工作。

Note: I am using: 注意:我正在使用:

PyCharm 2018.1 (Community Edition) PyCharm 2018.1(社区版)

Build #PC-181.4203.547, built on March 26, 2018 Build#PC-181.4203.547,建于2018年3月26日

JRE: 1.8.0_152-release-1136-b20 amd64 JRE:1.8.0_152-release-1136-b20 amd64

JVM: OpenJDK 64-Bit Server VM by JetBrains sro JVM:JetBrains sro的OpenJDK 64位服务器VM

Windows 10 10.0 Windows 10 10.0

and the code works fine in the IDE. 并且代码在IDE中正常工作。

EDIT : 编辑

My imports are: 我的进口是:

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

I am not adding any other import statements related to Qt. 我没有添加任何与Qt相关的其他导入语句。

EDIT - 2 : 编辑 - 2

Trying cx_Freeze instead of PyInstaller , here is the setup file. 尝试使用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)])

I added the additional_mods after executing the script once following ImportError , which are non-stop. 我在执行一次跟随ImportError的脚本之后添加了additional_mods ,这是不停的。 Any ways to hack and find which libraries I should mention explicitly? 任何方法来破解和找到我应该明确提到的库?

Additionally, I also tried to check which libraries are actually being imported when I run my main script with Qt using: 另外,当我使用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)

and apparently, it is importing PyQt5 internally . 显然,它是在内部导入PyQt5。 As mentioned before, I have NO import statements mentioning PyQt5. 正如前面提到的,我也没有 import语句提PyQt5。

Edit - 3 编辑 - 3

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. 因此,我将代码更改为 PyQt5 ,将pyinstaller更新为最新版本 - 3.4,现在有一个新问题,它找不到Qt插件。 It is still somehow importing PyQt4 and I don't know where. 它仍以某种方式导入PyQt4,我不知道在哪里。

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. 不是理想的情况,我不必将库更改为PyQt5并确保所有内容都与PyInstaller一致,但它确实有效。 So here is what I did: 所以这就是我所做的:

  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. 已安装的Python版本3.5 - 这是因为在我更新到最新的PyInstaller版本(3.4)并尝试在python 3.4上运行之后,我收到了一个新的错误,它无法找到Qt插件。 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 . 经过一番搜索后,我发现自从我在Python版本(3.4)上使用我在Python扩展软件包中找到的.whl文件安装PyQt5后,安装没有与sip捆绑在一起。 Additionally, when I tried to install PyQt5 on Python 3.4 using pip, it wouldn't install. 另外,当我尝试使用pip在Python 3.4上安装PyQt5时,它将无法安装。

  2. Installed PyQt5 and all other libraries on the new Python version using pip . 使用pip在新的Python版本上安装了PyQt5和所有其他库。 Note : This version of python doesn't have a PyQt4 installed, so it is quite possible that this was the reason. 注意 :这个版本的python没有安装PyQt4,所以很可能这就是原因。 I will install PyQt4 on this python version and try making the .exe again using PyInstaller and see what happens. 我将在这个python版本上安装PyQt4并尝试使用PyInstaller再次生成.exe,看看会发生什么。

So, to summarize, PyQt5 + Pyinstaller works only for Python version >= 3.5. 因此,总结一下,PyQt5 + Pyinstaller仅适用于Python版本> = 3.5。 Hope it helps others! 希望它能帮到别人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM