46

following are my python, qt and sip versions

root@thura:~# python -V
Python 2.7.3
root@thura:~# qmake --version
QMake version 3.0
Using Qt version 5.0.2 in /usr/lib/i386-linux-gnu
root@thura:~# sip -V
4.15.3

I tried to import the PyQt5 by following by this

from PyQt5.QtWidgets import QtGui, QtCore

I got the following error

ImportError: No module named PyQt5.QtWidgets

How can I solve this error.

Updated =====================

When I tried to PyQt4, I got following error.

from PyQt4.QtCore import pyqtSlot as Slot RuntimeError: the sip module implements API v10.0 to v10.1 but the PyQt4.QtCore module requires API v8.1

Updated 2013-12-20 ======================================

1) download sip-4.15.3.tar.gz from here

2) extract sip-4.15.3.tar.gz

3) copy sip-4.15.3 to /home/thura

4) type "cd /home/thura/sip-4.15.3"

5) type "python configure.py", press enter, follow the instructions (type yes and press enter)

6) type "make", press enter and type "make install", press enter

7) download PyQt-gpl-5.1.1.tar.gz from here

8) extract PyQt-gpl-5.1.1.tar.gz

9) copy PyQt-gpl-5.1.1 folder to /home/thura folder.

10) type "cd /home/thura/PyQt-gpl-5.1.1"

11) type "python configure.py", press enter, following the instructions (type yes and press enter)

12)type "make", press enter and type "make install", press enter

update 2013-12-20 =====================

After redo it again. I got the following error

make[2]: Entering directory `/home/thura/PyQt/qpy/QtDBus'
make[2]: Nothing to be done for `install'.
make[2]: Leaving directory `/home/thura/PyQt/qpy/QtDBus'
make[1]: Leaving directory `/home/thura/PyQt/qpy'
cd QtCore/ && ( test -e Makefile || /usr/lib/i386-linux-gnu/qt5/bin/qmake /home/thura/PyQt/QtCore/QtCore.pro -o Makefile ) && make -f Makefile install
make[1]: Entering directory `/home/thura/PyQt/QtCore'
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -fPIC -DSIP_PROTECTED_IS_PUBLIC -Dprotected=public -DQT_NO_DEBUG -DQT_PLUGIN -DQT_CORE_LIB -I/usr/share/qt5/mkspecs/linux-g++ -I. -I/usr/local/include/python2.7 -I../qpy/QtCore -I/usr/include/qt5 -I/usr/include/qt5/QtCore -I. -o sipQtCoreQtWindowStates.o sipQtCoreQtWindowStates.cpp
In file included from sipQtCoreQtWindowStates.cpp:24:0:
sipAPIQtCore.h:28:17: fatal error: sip.h: No such file or directory
compilation terminated.
make[1]: *** [sipQtCoreQtWindowStates.o] Error 1
make[1]: Leaving directory `/home/thura/PyQt/QtCore'
make: *** [sub-QtCore-install_subtargets-ordered] Error 2
Thu Ra
  • 2,013
  • 12
  • 48
  • 76
  • Did you install PyQt5? It doesn't come bundled with Qt5. – Blender Dec 19 '13 at 03:22
  • Yes I installed PyQt5 by following http://pyqt.sourceforge.net/Docs/PyQt5/installation.html – Thu Ra Dec 19 '13 at 03:28
  • So How can I bundle with Qt5? – Thu Ra Dec 19 '13 at 03:41
  • Are you using ubuntu? If so, which version? Do you have python3 installed as well as python2? (BTW: the import statement is wrong, it should be: `from PyQt5 import QtCore, QtGui, QtWidgets`). – ekhumoro Dec 19 '13 at 06:05
  • I'm using ubuntu 12.04 LTS. When I tried as ur import, I got "ImportError: No module named PyQt5" – Thu Ra Dec 19 '13 at 07:07
  • for python3, yes. When I tried python3 at console, there it is. Is it a problem? When I tried ** >>> import sip >>> print sip File "", line 1 print sip ** – Thu Ra Dec 19 '13 at 07:09
  • @ThuRa. The reason I asked about python3, is that the error messages clearly show you _did not_ install PyQt5 with python2. So have a look in `/usr/lib/python3.x/site-packages`, or just try importing PyQt5 in a python3 console. – ekhumoro Dec 19 '13 at 18:45
  • @ekhumoro, under /usr/lib/, there are 3 python folders. python2.7, python3 and python3.2. under python 2.7/dist-packages, I found PyQt4. But under python3/dist-packages, I did not find either PyQt4 or PyQt5. And under python3.2, I did not find either PyQt4 or PyQt5. And I found python2.7 and python3.2 under /usr/local/lib folder. But under python3.2, I found the dist-packages only but it is empty. And under the python2.7/dist-packages, I found the packages which I installed (Django, tornado, etc...). So, you mean, I have to move the PyQt5 into /usr/local/lib/python2.7/dist-packages, right? – Thu Ra Dec 20 '13 at 02:31
  • @ThuRa. Looks like we're getting somewhere. Don't move anything. If you start a python2 console session with `/usr/local/bin/python2.7`, you should be able to do `from PyQt5 import QtCore`. I would advise you to leave everything as it is under `/usr/local`, and use the python installed there for your dev work. It's best to leave your system pythons alone, as it is easy to break other ubuntu packages that rely on those specific versions of python. – ekhumoro Dec 20 '13 at 03:13
  • OK, I didn't move anything to anywhere. I'm using eclipse(kepler), in my coding, when I use **# /usr/local/bin/python2.7** and **from PyQt5 import QtCore** , I got **ImportError: No module named PytQt5**. – Thu Ra Dec 20 '13 at 04:08
  • If `from PyQt5 install QtCore` fails with all the pythons on your system, then it means you did not install PyQt5 properly. Please update your question with the _exact_ commands you used to try to install it. – ekhumoro Dec 20 '13 at 06:12
  • @ekhumoro, after redo from start, I got some error. I updated my question. Please look at and help me to solve it. – Thu Ra Dec 20 '13 at 08:48

9 Answers9

41

If you are on ubuntu, just install pyqt5 with apt-get command:

    sudo apt-get install python3-pyqt5   # for python3

or

    sudo apt-get install python-pyqt5    # for python2

However, on Ubuntu 14.04 the python-pyqt5 package is left out [source] and need to be installed manually [source]

Community
  • 1
  • 1
qtopierw
  • 1,447
  • 14
  • 23
32

pip install pyqt5 for python3 for ubuntu

Durgesh Kumar
  • 935
  • 10
  • 17
17

this can be solved under MacOS X by installing pyqt with brew

brew install pyqt
tom
  • 1,647
  • 14
  • 15
8

After getting the help from @Blender, @ekhumoro and @Dan, I understand the Linux and Python more than before. Thank you. I got the an idea by @ekhumoro, it is I didn't install PyQt5 correctly. So I delete PyQt5 folder and download again. And redo everything from very start.

After redoing, I got the error as my last update at my question. So, when I search at stack, I got the following solution from here

sudo ln -s /usr/include/python2.7 /usr/local/include/python2.7

And then, I did "sudo make" and "sudo make install" step by step. After "sudo make install", I got the following error. But I ignored it and I created a simple design with qt designer. And I converted it into python file by pyuic5. Everything are going well.

install -m 755 -p /home/thura/PyQt/pyuic5 /usr/bin/
strip /usr/bin/pyuic5
strip:/usr/bin/pyuic5: File format not recognized
make: [install_pyuic5] Error 1 (ignored)
Community
  • 1
  • 1
Thu Ra
  • 2,013
  • 12
  • 48
  • 76
4

This probably means that python doesn't know where PyQt5 is located. To check, go into the interactive terminal and type:

import sys
print sys.path

What you probably need to do is add the directory that contains the PyQt5 module to your PYTHONPATH environment variable. If you use bash, here's how:

Type the following into your shell, and add it to the end of the file ~/.bashrc

export PYTHONPATH=/path/to/PyQt5/directory:$PYTHONPATH

where /path/to/PyQt5/directory is the path to the folder where the PyQt5 library is located.

Dan
  • 12,157
  • 12
  • 50
  • 84
  • When I tried export ....., I got following root@thura:/home/thura/PyQt5# ~/.bashrc -bash: /root/.bashrc: Permission denied root@thura:/home/thura/PyQt5# export PYTHONPATH=/home/thura/PyQt5:$PYTHONPATH. But I still get the no module error. – Thu Ra Dec 19 '13 at 04:10
  • @Thu Ra: What's in `/home/thura/PyQt5`? It might not contain the actual binary or python module you need to link against, just the directory that contains it. – Dan Dec 19 '13 at 05:23
  • I downloaded the PyQt-gpl-5.1.1.tar.gz from the http://pyqt.sourceforge.net/Docs/PyQt5/installation.html. And I installed it as it's instruction. – Thu Ra Dec 19 '13 at 07:05
  • @Thu Ra: That's not what I said. `/home/thura/PyQt5` may not be the directory that python needs to link to. It may have a subdirectory that contains the actual library. You need the name of that subdirectory, *not* `/home/thura/PyQt5`. – Dan Dec 19 '13 at 07:54
  • there is so many subdirectory. But there is not a name like lib or library. Pls tell me which dir to pin point. – Thu Ra Dec 19 '13 at 08:18
  • @ Dan, I've a important proj. So, I want to uninstall or downgrade PyQt5 and sip to previous version. How can I do? Please help me. – Thu Ra Dec 19 '13 at 09:48
  • @ Dan, Do you mean **/usr/lib/python2.7/site-packages** ? – Thu Ra Dec 19 '13 at 11:11
  • Try looking for the library with `ls */*.*o */*.py` – Dan Dec 19 '13 at 15:17
  • when I tried `ls */*.*o */*.py` as root, I got following **ls: cannot access */*.py: No such file or directory lib/klibc-LZ1cv1NoEVO2ugnvqTw3e4qPc8Y.so lib/libproc-3.2.8.so root/hello.o** . What will I do? – Thu Ra Dec 20 '13 at 02:38
  • @Thu Ra: What directory were you in? – Dan Dec 20 '13 at 03:16
  • I run this `ls */*.*o */*.py` as root user at root folder. – Thu Ra Dec 20 '13 at 04:10
  • @Thu Ra: You want to run it in `/home/thura/PyQt5`. You don't need to be root. – Dan Dec 20 '13 at 07:00
  • When I run it at /home/thura/PyQt5 without root access, it show me so many .pro files and configure.py. – Thu Ra Dec 20 '13 at 07:28
  • Try this then: `ls */*.so */*.o */*.py`. Are there any files ending in just `.so` or `.o`? – Dan Dec 20 '13 at 15:08
3

How to install PyQt5 in Python3

Just installing it did not work for me. I had to uninstall it first, then reinstall it:

# upgrade pip
python3 -m pip install --upgrade pip

# uninstall
python3 -m pip uninstall PyQt5
python3 -m pip uninstall PyQt5-sip
python3 -m pip uninstall PyQtWebEngine

# reinstall
python3 -m pip install PyQt5
python3 -m pip install PyQt5-sip
python3 -m pip install PyQtWebEngine

See here where I learned this: Python 3.7.0 No module named 'PyQt5.QtWebEngineWidgets'

If using a specific version of Python3, and the above doesn't work, you may need to specify the exact version of Python3 like this. Here I am specifying Python3.8, for instance:

python3.8 -m pip install --upgrade pip

python3.8 -m pip uninstall PyQt5
python3.8 -m pip uninstall PyQt5-sip
python3.8 -m pip uninstall PyQtWebEngine

python3.8 -m pip install PyQt5
python3.8 -m pip install PyQt5-sip
python3.8 -m pip install PyQtWebEngine
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
2

On windows, "pip install pyqt5", solved it for me.

1

You can try to Open the anaconda-prompt with Admininistator user option; conda install pyqt=5

Bangash Owais
  • 61
  • 1
  • 7
0

It may caused by different python version, check which version of python you are using, for me the global version was 2.7 and the python version installed in the virtual environment was 3.8, so there was difference, so I run the main.py inside the environment and it works.

enter image description here

Nasser Ali Karimi
  • 4,462
  • 6
  • 34
  • 77