Installation of PyQt on Mac OS X

 | May 12, 2009 5:12 pm

Note: The installation instructions in this article apply to Mac OS X 10.5 (Leopard) and Qt 4.5.3.  While it is possible to get Qt 4.5.3 to run on Snow Leopard, it requires compiling it from source.  Instructions for doing so can be found in the comments below.  It is important to note, however, that there are some minor issues with the 4.5 framework on Snow Leopard (including mutlithreaded memory leaks).

You can find instructions for how to install PyQt on Snow Leopard using the Qt 4.7 frameworks at http://blog.oak-tree.us/index.php/2010/05/27/pyqt-snow-leopard.

To install PyQt on Mac OS X, it is also necessary to install and configure the GCC compilers and the Qt framework in addition to SIP and PyQt.  The easiest way to get the most current version of the GCC compilers and tools is by downloading the XCode tools package from Apple at the Apple Developer Connection (ADC).  ADC requires that you have an Apple ID and account, which are both free.  After you log-in, download the XCode developer tools package, which is a bit hefty (nearly 1 GB in size) and may take a while to download depending on your internet speed.

XCode and Qt

While waiting for the XCode tools to download, you can also begin downloading the Qt binary installer from the Trolltech downloads site.  As with Windows, I would advise that you you select  the complete SDK (which also include pre-compiled binaries) rather than the source release.  If you only download the Qt framework, you will need to compile the source yourself which is both time intensive and is not worth the additional headache.

Once XCode has finished downloading, run the package installer.  It will ask you to select the destination drive and what components of the software you would like to install.

Select the installation disc where you want to install XCode the developer tools

Choose the components you would like to install

This will automatically install and configure the GNU tool chain.  Once it has finished, you can access the tools by going to the “/Developer/” folder on the root of the drive.

After XCode has finished, run the Qt installer.

The Qt SKD Installer

Select the installation destination

The Qt installer package will automatically place the tools and other things that you will need in the same Developer folder where the XCode tools and frameworks are located.  You will need to make a note of the path for qmake.  The default installation site for qmake is “/usr/bin/qmake-4.5”.

SIP and PyQt

SIP is a program that makes it easy to create C++ bindings program for Python.  You can find the source code at Riverbank’s SIP download page.  Since we will be using the 4.5 version of PyQt, you will need to download the latest developer snapshot (version 4.8 or greater).  Be sure to get the Linux/Unix source code, rather than the Windows source.  You will also need the latest source code snapshot for PyQt 4.5, which is also available from Riverbank.

After you have finished downloading the source files, move them to a folder in your Users directory.  I have a special directory entitled “Applications” where I keep the source code for programs that I have manually compiled.  Note: The rest of the steps will be done from the Mac OS X terminal.

After you have moved the source code for both SIP and PyQt to this new directory, extract it by using the tar utility with the x and f options (tar –xf):

cd ~/Applications
tar -xf sip-4.8-snapshot-20090430.tar
tar -xf PyQt-mac-gpl-4.5-snapshot-20090507.tar

After you have expanded the files, it might be a good idea to rename the directories to something more manageable (like sip-4.8 and PyQt4.5:

mv sip-4.8-snapshot-20090430 sip-4.8
mv PyQt-mac-gpl-4.5-snapshot-20090507 PyQt4.5

First, we need to compile and install SIP.  The default configuration will move the compiled files to a directory where Leopard can’t read them.  So, we will manually need to specify the destination directory using the –d flag:

cd sip-4.8
python configure.py -d /Library/Python/2.5/site-packages

After the configuration is finished, run make and sudo make install:

make
sudo make install

Once SIP has finished installing, we need to repeat the process for PyQt.  From the sip-4.8 directory, chagne to PyQt:

cd ..
cd PyQt4

Next, run the configuration script specifying the path to your version of qmake and the installation directory for the python bindings:

python configure.py -q /usr/bin/qmake-4.5 -d /Library/Python/2.5/site-packages/

Then compile and install:

make
sudo make install

Since Qt is a rather large framework, it may require between 15 and 30 minutes to fully compile.

Similar Posts:

12 Responses to “Installation of PyQt on Mac OS X”

[...] Art and Photography « Installation of PyQt on Mac OS X [...]

Scott wrote a comment on June 16, 2009

Thanks for the tutorial, it was extremely helpful! I'm just having a small issue including PyQt libraries. Everything is present in /Library/Python/2.5/site-packages/PyQt4 such as the .so files, but my script still won't run. It's almost as if it's not finding these libraries because this is the error I'm getting:

from PyQt4 import QtCore, QtGui
ImportError: cannot import name QtCore

I've checked sys.path and site-packages is listed so I'm kinda at a loss here. Any ideas? Thanks!

Rob Oakes wrote a comment on June 16, 2009

@Scott. Thanks for the feedback, I'm glad that you found the tutorial to be useful. After reading through your experience, I had a few thoughts about where the problem may lie:

1) It may not be an issue with your python installation, but rather with your Qt installation. I've been trying to compile Qt on Cygwin and while it will build and install, it doesn't work correctly. Thus, when I try and build PyQt against it, the build process fails. One way to validate your Qt installation would be to try and compile a few of the demo programs. These can be found in the documentation folder. If you installed using the Qt 2009.02 SDK, you'll find it under /Developer/Examples/Qt.

2) If you have other versions of Qt installed, make sure that pyqt found the correct version of qmake. Though I don't know what it does, Leopard appears to have a version of qmake that is either bundled with the system or comes with XCode. This version of qmake doesn't appear to build against the appropriate frameworks. When configuring pyqt, make sure to use the -q flag and reference /usr/bin/qmake-4.5 rather than /usr/bin/qmake. It makes an important difference. Example:

python configure.py -q /usr/bin/qmake-4.5 -d /Library/Python/2.5/site-packages/

One last thought, if you have more than one version of python installed (say from MacPorts or MacPython), make sure that the correct version of python is being referenced. The instructions I give above are for the native install of Python. (But it sounds like you've already checked this via sys.path.) I've never liked using MacPorts/MacPython, since they tend to wreak havoc with my system.

Best of luck,

Rob

Scott wrote a comment on June 18, 2009

Thanks for the tips Rob, but I'm really just at a wall. I think my problem is that I Installed MacPython 3.0 and even after removing it and redoing everything, including compiling 2.63 and compiling PyQt into that it still won't work. I've tested some Qt demo apps with XCode and they worked perfectly, so it doesn't appear to be a problem with my Qt installation. It's something to do with the way I compiled sip and PyQt. I'll post if I find a solution, but I'm still open to suggestions. Thank you!

Scott wrote a comment on June 20, 2009

Woohoo! I finally got it. Turns out PyQ4 4.5 was buggy, so I compiled the latest version, 4.5.1, which was released on June 16th. It worked on the first try. Sometimes it just pays to wait for the next version.

[...] where should you leave notes on how to set-up and configure your home server, the instructions for installing PyQt on Mac OS X for the first time, or the essential (though obscure) Subversion commands that you infrequently [...]

Moses Ting wrote a comment on July 12, 2009

Thanks for the wonderful instructions. In the process of installing on Mac OS X Leopard!

Mark Fickett - PyQt4 Installation on Snow Leopard wrote a comment on September 17, 2009

I found that the out-of-the-box installation does not work on MacOS X Snow Leopard. Here's what I did that eventually did get it to work.

(My default python is 2.6. Note that during installation for Qt and PyQt4, there were lots of warnings that 'Support for this version of Mac OS X is still preliminary'.)

I. What worked

tar xvfz qt-mac-opensource-src-4.5.2.tar.gz
cd qt-mac-opensource-src-4.5.2
./configure -arch x86_64 -opensource
make
sudo make install

At this point, you can run the Qt demo - a necessary, but not sufficient condition for PyQt4.
$ open /usr/local/Trolltech/Qt-4.5.2/bin/qtdemo.app

SIP is a prerequisite for PyQt:
tar xvfz sip-4.9-snapshot-20090906.tar.gz
cd sip-4.9-snapshot-20090906
python configure.py
make
sudo make install

tar xvfz PyQt-mac-gpl-4.6-snapshot-20090906.tar.gz
cd PyQt-mac-gpl-4.6-snapshot-20090906
python configure.py --verbose -j 1 --no-designer-plugin -q /usr/local/Trolltech/Qt-4.5.2/bin/qmake
make
sudo make install

And now you can run the PyQt version of the demo application:
$ python examples/demos/qtdemo/qtdemo.py

II. System details

Mac OS X, Version 10.6.1
MacBook Pro, Intel Core 2 Duo
Python 2.6.1 (from Apple)
Compilation tools from XCode install:
$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646)

III. Various things that didn't work

The package installer from Qt for Mac OS X didn't work (it sat for a while preparing, and then failed; the console had a message about not being able to communicate with a daemon).

After installing Qt with the default ./configure, PyQt configuration doesn't succeed for anything except QtCore, and importing QtCore doesn't work. After installing Qt 4.5.2 as I installed 4.6, PyQt configuration succeeds, but there are compilation errors.

sanchox wrote a comment on December 28, 2009

hello there, thanks for the detailed writeup...

I am having an issue completing the installation process :( i am on a g5 running 10.5.8. I have followed everything to a T but when i run this command: python configure.py -q /usr/bin/qmake-4.6 -d /Library/Python/2.6/site-packages/

i get this: Determining the layout of your Qt installation...
Error: qtdirs.app/Contents/MacOS/qtdirs failed to create qtdirs.out. Make sure
your Qt v4 installation is correct.

i did a quick search and found the app qtdirs and noticed that it is not compiled for the ppc architecture... i imagine this is the root of the problem, and after a few minutes of googling, i have found nothing that helps. any chance you have a solution?

thanks for your time!

Steve Steiner wrote a comment on January 14, 2010

I had to modify the PyQT build line to:

# python configure.py -w -q /usr/local/Trolltech/Qt-4.6.1/bin/qmake

Not sure which part of the original command was causing a problem. Now I get an error on sip.so being the wrong arch.

python examples/demos/qtdemo/qtdemo.py
Traceback (most recent call last):
File "examples/demos/qtdemo/qtdemo.py", line 46, in
import sip
ImportError: dlopen(/Users/ssteiner/.virtualenvs/py-2.6.4-1/lib/python2.6/site-packages/sip.so, 2): no suitable image found. Did find:
/Users/ssteiner/.virtualenvs/py-2.6.4-1/lib/python2.6/site-packages/sip.so: mach-o, but wrong architecture

I'm a couple of versions up on everything (QT 4.6.1, sip-4.9.3, PyQt-mac-gpl-4.6.2)

Will come back with a solution if I find one.

Steve Steiner wrote a comment on January 14, 2010

# python configure.py --arch=i386 -d /Library/Python/2.6/site-packages

And sip imports fine. NOTE: I'm running Python 2.6.4 installed from the python.org .dmg file install, which it turns out is 32 bit so it seems that the auto-detect of x86_64 is wrong and I may have to rebuild *everything*.

Tore Torset wrote a comment on March 16, 2010

Hi, thanks for the guide!

I do however have some problems making it work on Snow Leopard (10.6.2). I'm quite new to a lot of this, so I hope someone can help me out

I'm using:
Python 2.6.4
Sip-4.10
PyQt-mac-gpl-4.7
qt-mac-cocoa-opensource-4.6.2

Other QT verions did not work (error: Failed to determine the layout of your Qt installation when trying to configure PyQt)

Now however, I am stuck on a different error. When trying to configure PyQt i get the following error:
Error: Unable to create the C++ code.

Ive tried to rebuild sip using -arch i386 as well, but I'm not sure if it is sip causing this...
Can it be a problem of the Qt install?

I've tried configuring PyQt using –verbose -j 1 –no-designer-plugin -q as proposed above by mark, or as propsed by original poster and also --use-arch i386. All return the same error of not being able to create the c++ code

Could compiling the Qt installation work?
When browsing ftp://ftp.qt.nokia.com/qt/source/ for source versions, I could'nt find the mac version src for 4.6.x... Perhaps someone could point me to the correct source to use for 4.6.x and higher?

Any hints and tips is appreciated!
Will