Setting up Python, PyQt4 and Twisted on OSX 10.6 Snow Leopard

// March 11th, 2011 // Posted in OS X, PyQT4, Python // Tagged as , ,

I am trying to use Snow Leopard more often as my alternate platform of choice for Python programming, even though at the moment I still spend majority of my coding time on Linux.

My first step would be on how to make Python libraries that I use frequently, such as Twisted and PyQt4, available on the platform. The good thing is on Snow Leopard the default installation has Python 2.6.1 along with Twisted installed ( and I think Python 2.5.4 by default is there as well). Soon you’ll found out that we actually don’t need the default installed Python.

To check whether Twisted is installed properly or not we just have to go to the Python console and try to load the Twisted library. Error will be thrown if the library is not in there.

First we need to launch Python console from the Terminal

pietra@unix$ python

That should launch the default shipped Python interpreter. Next, from Python console type these:

>>> from twisted.internet import reactor
>>> reactor.run()

It will launch the Twisted’s reactor event loop. Press Ctrl-C to stop the loop then Ctrl-D to quit the Python console. In the UNIX world Ctrl-D basically means End-of-Transmission or EOT.

At this point if you are developing Python and Twisted only then everything is already in place and no need for further customization. Complication arises as soonest you put PyQt4 into the mix because by default it’s not shipped with Snow Leopard. On top of that try to add MacVim as IDE along with Python syntax completion/highlighter and things can get a bit messy.

In order to install PyQt4 we can opt for the hard route, which is manually compiling the source, or as what I would do is to install it from MacPorts. If you’re not familiar with MacPorts, here the quick intro taken from their site: “The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X“. In my case MacPorts is critical to maintain compatibility between Python 2.6 runtime, Twisted, PyQt4, and MacVim with Python and Perl code completion plus syntax highlight.

Go grab the installer from MacPorts download page and install it. Which installation method you prefer is up to you but as for me I just grabbed the “dmg” for Snow Leopard and install the pkg from there.

During the installation process MacPorts should update it self to the latest. If for some reason it won’t or in case you already have MacPorts installed, all you have to do is manually update it to stay current. From Terminal type:

pietra@unix$ sudo port -v selfupdate

Next we need to install Python 2.6, PyQt4 and Twisted using MacPorts. It will automatically fetch all the source codes and compiles them for you. This process will take quite sometime because you actually built everything from scratch. First let’s install MacPorts version of Python 2.6

pietra@$unix> sudo port install python26

The MacPorts version of Python 2.6 will coexists with Snow Leopard’s default Python. Which Python becomes the default is callable by “/usr/bin/env python”. To be able to switch between Python versions we need to have python_select installed.

pietra@unix$ sudo port install python_select

At this point if you launch “python” from Terminal you should have the MacPorts version of it. Usually its newer than the Snow Leopard’s default which is 2.6.1.

We also need to install MacPorts version of PyQt4 and Twisted

pietra@unix$ sudo port install py26-pyqt4
pietra@unix$ sudo port install py26-twisted

Next step is to make sure that PyQt4 and Twisted are installed properly. From Python console:

>>> from PyQt4 import QtCore
>>> print QtCore.PYQT_VERSION_STR

That should prints out the installed version of PyQt4 on your machine. As for Twisted just do the things that i’ve put at the beginning of this page to check for Twisted installation.

After this you might want to setup proper IDE for your Python development. I suggest you read my other journal regarding it: “MacVim as IDE with Code Completion and Syntax Highlighting for Python, PyQt4 and Twisted Development on Mac OSX Snow Leopard”

Ads by Amazon.com:

Please share this journal with others:

5 Responses to “Setting up Python, PyQt4 and Twisted on OSX 10.6 Snow Leopard”

  1. […]Lovely just what I was searching for.Thanks to the author for taking his time on this one[…]…

  2. LetSpeeklykek says:

    Hello! Just want to say thank you for this interesting article! =) Peace, Joy.

  3. Homepage says:

    … [Trackback]…

    […] Read More here: phacks.net/python-pyqt-twisted-on-osx-snowleopard/ […]…

  4. Weaver says:

    Thanks man. I was looking for just this.

  5. Don Campbell says:

    hey man.. this is the 2nd time that I have used your info to set up pyqt on my mac….thanks documenting this!

Leave a Reply