PyQt5 failing import of QtGui


I’ve just moved from PyQt4 to 5 and I’m having an issue with QtGui. I installed using the 32bit windows installer, not my own build.

when I do:

from PyQt5 import QtGui

I get

class MainWindow(QtGui.QMainWindow, UI.MainUI.Ui_MainWindow):
AttributeError: 'module' object has no attribute 'QMainWindow'

so I tried

from PyQt5.QtWidgets import QtGui

Which results in:

ImportError: cannot import name QtGui

then I tried to change the sys.path according to Pyinstaller: ImportError: cannot import name QtGui work around but it still gives me the same

ImportError: cannot import name QtGui

Update: It looks like I do in fact import QtGui because when I go in IDLE and try it, it still autocompletes QMovie and a whole bunch of other attributes. Is there any reason QMainWindow just wouldn’t be in there? (It’s not, neither is QDialog and they seem important)

Asked By: Faller



Assuming everything was installed correctly, you need to adjust your imports slightly to port from PyQt4 to PyQt5.

The main GUI elements are in the QtWidgets module, whilst the more basic GUI elements are in QtGui. See the Qt modules page for more details.

The example code needs to be changed to something like:

from PyQt5 import QtCore, QtGui, QtWidgets

class MainWindow(QtWidgets.QMainWindow, UI.MainUI.Ui_MainWindow):

For more details on porting from PyQt4 to PyQt5, see: Differences Between PyQt4 and PyQt5.

Answered By: ekhumoro
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.