11

Is there some reason that QtGui is packaged with PyQt5???

I am using Mark Summerfield's Rapid GUI programming book. Obviously this book was written with Qt4 in mind, but I have been recommended to use Qt5, and PyQt5. I want to run the first example code in chapter 4. The code begins with the following import statements:

import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtGui import *

To which the compiler responds:

Traceback (most recent call last):
  File "wakeUp.py", line 4, in <module>
    from PyQt5.QtGui import *
ImportError: No module named 'PyQt5.QtGui'

Note that the PyQt5.QtCore import statement does not generate an error.

From the terminal,

$ echo $PYTHONPATH
:/usr/lib/python3.3/site-packages

Has anybody else come across this import error for QtGui?

From an interactive session I can

>>> import PyQt5.Qt
>>> import PyQt5.QtDBus
>>> import PyQt5.QtNetwork
>>> import PyQt5.QtXmlPatterns

But I don't have QtWidgets, QtGui, QtWebkit, QtDesigner, and several others.

Also, if it is helpful, the contents of /usr/lib/python3.3/site-packages/PyQt5 are:

__init__.py  QtCore.so  QtDBus.so  QtNetwork.so  Qt.so  QtXmlPatterns.so  uic

which are the same modules that I am able to import. Should the other modules (QtWidgets, QtGui etc) be here too?

I am using Ubuntu 13.04 and Python 3.3.

mata
  • 67,110
  • 10
  • 163
  • 162
ADB
  • 1,210
  • 4
  • 15
  • 23
  • May be you should use PyQt5.QtWidgets instead of QtGui? See [this example](http://pyqt.sourceforge.net/Docs/PyQt5/python_shell.html). – Pavel Strakhov Jun 30 '13 at 08:44
  • Yes, I had seen that. Unfortunately QtWidgets is also giving the same import error. – ADB Jun 30 '13 at 15:39
  • If example from the docs doesn't work then pyqt installation is wrong or incomplete. Please provide some information about your OS and pyqt installation. – Pavel Strakhov Jun 30 '13 at 15:42

6 Answers6

7

When first trying pyqt4 and pyqt5 and the pycharm IDE I had many problems with imports. (although the imports had no prob running from IDLE) Eventually after much stuffing around, uninstalling and reinstalling, (including dependencies) the imports sorted themselves out.

Did you install pyqt5 using an installer from the pyqt website? You must. Qt designer is found under the start menu in windows. When following PyQt4 tutorials, I have had luck using the following import statements for PyQt5...

from PyQt4 import QtCore, QtGui #in the pyqt4 tutorials
from PyQt5 import QtCore, QtGui, QtWidgets #works for pyqt5

I'm new to it myself but in pyqt4, QtWidget was part of QtGui (QtGui.QtWidget), in pyqt5 it gets imported by itself as QtWidgets. Its only a small change in code to get the pyqt4 tutorials working in pyqt5.

I personally went back to pYqt4 to take advantage of pyqtgraph.

Look at my pyqt4 post here which walks you through using Qt Designer.

Community
  • 1
  • 1
Ninga
  • 689
  • 7
  • 14
  • "Did you install pyqt5 using an installer from the pyqt website? You must." This answer, if it was ever true, is certainly out of date. The best way to install pyqt5 is either with pip into a virtualenv, which will get a binary wheel, or with Anaconda's package manager. Qt's own installer will not restrict itself neatly to a local environment. – Arthur Tacca Jan 28 '20 at 16:11
4

The problem was when I was running the PyQt5 configure script. The correct option to pass went like this:

> python3 configure.py --qmake [path to Qt5.x]/bin/qmake

I was providing the path up to bin, but did not specifically point to qmake. Go figure!

After running the configure script like this, I was able to import all the PyQt5 modules.

ADB
  • 1,210
  • 4
  • 15
  • 23
  • When installing in a venv with "pip install PyQt5", there is no qmake folder or file. I can find \venv\Lib\site-packages\PyQt5\Qt\bin, but it is full of dll's, no qmake. So installing from the wheels for python3.7 is not the right way to install it? – Echeban Nov 10 '19 at 11:56
4

Configure pyqt with this command:

python[3] configure.py --qmake=[path to Qt5.x]/bin/qmake --verbose

Whether a qt module builds or not depends on the configure.py's check. Take a look at the terminal output and you will find the reason why QtGui..QtWidgets was not installed correctly.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
kentop
  • 41
  • 2
  • right on! I didn't have the GL libraries installed so QtGui was not building. a `sudo apt install libgl-dev` did the trick for me--after that configure set up everything correctly. – DenverCoder9 Feb 07 '17 at 01:24
0

same issue, python run py ok, but in all IDE: visual studio code/PyCharm. It will show syntax error.

Finally I solved it by set env var PYTHONPATH to python site-packages path, e.g.

PYTHONPATH=D:\Anaconda3\Lib\site-packages
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
raidsan
  • 777
  • 1
  • 7
  • 11
0

I had this problem today and I solved as below:

Instead of importing like:

from PyQt5.QtGui import QIcon

Imported this way and it will just fine:

from PyQt5 import QtGui
from PyQt5.QtGui import QIcon
Julio S.
  • 944
  • 1
  • 12
  • 26
-3

in PyQt5, QtGui and QtCore is located into the QtWidgets

You should import like this:

from PyQt5.QtWidgets import QtGui, QtCore

instead of

from PyQt5.QtCore import *
from PyQt5.QtGui import *

Hope this helps. :)

Surinder ツ
  • 1,778
  • 3
  • 16
  • 27
  • 1
    hi, in PyQt5, your following code from `PyQt5.QtWidgets import QtGui, QtCore` returns this error ImportError: cannot import name QtGui – Rhys Oct 20 '13 at 10:40
  • 1
    @Rhys check you are using PyQt5 or PyQt4. This solution works for PyQt5 only. – Surinder ツ Oct 20 '13 at 12:37
  • **PyQt5.5.1 and Qt5.6**: `from PyQt5.QtWidgets import QtGui, QtCore` => *ImportError: cannot import name 'QtGui*'. This works: `from PyQt5.QtWidgets import QWidget` and `import PyQt5.QtCore as QtCore`. If you actually need the QtGui module: `import PyQt5.QtGui as QtGui` – 7stud Apr 24 '16 at 09:25