30

This question was originally asked in askubuntu.com but got no attention so I think maybe this is a better place to ask.

I installed PyQt4 with synaptic.

I'm using Python3 so I need to configure the path for PyQt4 in eclipse, but it seems synaptic only installed PyQt4 for python 2 since I only found relative files and folders under python 2.6 and 2.7.

So how can I make PyQt4 to work with Python 3 and eclipse ?

Thanks.

UPDATE

I tried to configure it following this post: http://ubuntuforums.org/showthread.php?p=10914908#post10914908

But after following all the instructions without any error occurs in the way, I get error from running this code in python 3.2:

>>> import PyQt4

The error message is :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4

But the strange thing is that the same code results NO error in python 2.7 (I have both 2.7 and 3.2 installed on my machine)

Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73

3 Answers3

70

You should be able to install the python 3 version of PyQt4 in ubuntu. Open a terminal and type:

sudo apt-get install python3-pyqt4

This way you don't have to compile Qt4 from source. I have also tested this and pyqt4 works with python3.

Source: A quick search in the ubuntu repositories.

Connor Blakey
  • 846
  • 1
  • 7
  • 8
31

Install packages needed for compiling (I am not sure these all are needed):

sudo apt install build-essential python3-dev libqt4-dev

Download sources of the latest SIP - sip-4.12.4.tar.gz (Linux, UNIX, MacOS/X source).

Unpack them and enter the directory:

vic@wic:~/Desktop/sip-4.12.4$ python3 configure.py 
This is SIP 4.12.4 for Python 3.2 on linux2.
The SIP code generator will be installed in /usr/bin.
...
Creating sip module Makefile...

vic@wic:~/Desktop/sip-4.12.4$ make
make[1]: Entering directory `/home/vic/Desktop/sip-4.12.4/sipgen'
...
make[1]: Leaving directory `/home/vic/Desktop/sip-4.12.4/siplib'

vic@wic:~/Desktop/sip-4.12.4$ sudo make install
make[1]: Entering directory `/home/vic/Desktop/sip-4.12.4/sipgen'
...
cp -f /home/vic/Desktop/sip-4.12.4/sipdistutils.py /usr/lib/python3/dist-packages/sipdistutils.py

vic@wic:~/Desktop/sip-4.12.4$

Download the sources of the latest PyQt - PyQt-x11-gpl-4.8.5.tar.gz (Linux, UNIX source), and install it:

vic@wic:~/Desktop/PyQt-x11-gpl-4.8.5$ python3 configure.py 
Determining the layout of your Qt installation...
This is the GPL version of PyQt 4.8.5 (licensed under the GNU General Public License) for Python 3.2 on linux2.

Type '2' to view the GPL v2 license.
Type '3' to view the GPL v3 license.
Type 'yes' to accept the terms of the license.
Type 'no' to decline the terms of the license.
Do you accept the terms of the license? yes
Found the license file pyqt-gpl.sip.
Checking to see if the QtGui module should be built...
...
Checking to see if the dbus support module should be built...
The Python dbus module doesn't seem to be installed.
Qt v4.7.2 free edition is being used.
SIP 4.12.4 is being used.
The Qt header files are in /usr/include/qt4.
...
Creating pyqtconfig.py...

vic@wic:~/Desktop/PyQt-x11-gpl-4.8.5$ make
make[1]: Entering directory `/home/vic/Desktop/PyQt-x11-gpl-4.8.5/qpy'
...
make[1]: Leaving directory `/home/vic/Desktop/PyQt-x11-gpl-4.8.5/designer'

vic@wic:~/Desktop/PyQt-x11-gpl-4.8.5$ sudo make install
make[1]: Entering directory `/home/vic/Desktop/PyQt-x11-gpl-4.8.5/qpy'
...
cp -f PyQt4.api /usr/share/qt4/qsci/api/python/PyQt4.api

Note, that I am launching python3 instead of python.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
3

It looks like ubuntu only has pyqt4 packages for python2, so you will have to compile a separate pyqt4 for python3.

You can check what you already have installed by looking in the site-packages directory for each python. To locate these directories, run:

python2.7 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
python3.2 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"

If pyqt is installed properly, there should be a PyQt4 directory under site-packages containing a couple of dozen *.so files.

Presumably, there will be nothing there for python3.2, which is why you can't import pyqt.

To install pyqt for python3.2, follow these instructions. Please note that it is very important that you configure the build using the right python, e.g:

/usr/bin/python3.2 configure.py
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • The post I mentioned in my post is more detailed than the instruction you mentioned, and I also used python 3.2 to run the configure.py. But it still doesn't work properly. – Derrick Zhang Oct 30 '11 at 05:12
  • @SpiritZhang. Are the pyqt libs installed in the `site-packages` directory for python3.2? – ekhumoro Oct 30 '11 at 05:25
  • no, it's not there, and that's the problem. There are *.so files under both /usr/lib/pyshared/python2.7/PyQt4 and /usr/lib/python2.7/dist-packages/PyQt4. Does simply copying those files to folders of python3.2 help with the problem ? – Derrick Zhang Oct 30 '11 at 08:24
  • @SpiritZhang. The two paths you give are both for _python2.7_. Do not move or copy any of the files in those directories. It sounds like you have built and installed `sip`, but you have not built and installed pyqt itself. Note that the instructions you linked to don't really include instructions for building and installing pyqt, but the link in my answer does. Please read those instructions carefully before doing anything else, and make sure you understand what you need to do. If there's anything you don't understand, please ask. – ekhumoro Oct 30 '11 at 14:28