Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force PyQt5 use for QObject class?

I'm developping a small graphic application using Python 3 and PyQt5. On the first computer I use, where only PyQt5 is installed, everything in my code is fine. But when I want to run my code on my other laptop, where both PyQt4 and PyQt5 are installed, I get the following error:

RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

Python interpreter locates the error in the file "ViewWindow.py", called from the main file.

As I have both PyQt4 and PyQt5 on this laptop, and because I can't uninstall PyQt4 (it would be too easy...), I wonder if it's possible to force use of PyQt5.QtCore, or something else to avoid this problem. My configuration on this laptop: Debian 8, Python3.4, PyQt4 and 5 (without special configuration, installed from Debian repos), IDE = Spyder.

I put there first lines of my files main.py and ViewWindow.py.

# main.py
import sys
import sqlite3
import ViewWindow
from DataWindow import DataWindow
from PyQt5.QtCore import QObject # I tried adding this line, but nothing changed...
from PyQt5.QtWidgets import (QApplication,
                         QWidget,
                         QGridLayout,
                         QHBoxLayout,
                         QLabel,
                         QLineEdit,
                         QPushButton,
                         QTextEdit,
                         QVBoxLayout
                         )


class MainWindow(QWidget):
    # Some cool stuff


# ViewWindow.py
import sys
import sqlite3
from PyQt5.QtCore import QObject # same thing than above, adding this line doesn't change the output.
from PyQt5.QtWidgets import (QApplication,
                         QWidget,
                         QGridLayout,
                         QLabel,
                         QPushButton,
                         QVBoxLayout
                         )


class ViewWindow(QWidget):

Does someone knows how to make this code run ?

Thanks,

Jerry M.


Edit: I tried to run that script forcing use of Python3, and it worked... It seems that problem comes from iPython3. Thanks for your help.

like image 678
Jerry Magnin Avatar asked Oct 31 '22 02:10

Jerry Magnin


1 Answers

A RuntimeError with message

the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

is raised the moment you try to import PyQt5.QtCore while PyQt4.QtCore was already imported before.

This error is raised within SIP, which is used to connect to Qt. Like it states, it's only allowed to have one module claiming to wrap QObject. Thus the error just tells you, that you're using PyQt4 and PyQt5 at once.

So you need to find the module loading PyQt4 to configure it to use PyQt5 instead. Alternatively you could try to put from PyQt5.QtCore import QObject before any other import and hope, that the module, which usually imports from PyQt4, is adaptable and able to use PyQt5 as fallback.

like image 184
tynn Avatar answered Nov 14 '22 10:11

tynn