PyQt 4.8 + Static Qt 4.7 on OS X Lion

by AbiusX

(10 August 2011)-> Sample Code of this post

This is outdated, I now provide PyQtX binary distributions for OS X at
https://sourceforge.net/projects/pyqtx/files/

I spent some valuable time trying to bend things to work together, And I'm going to share the experience since
I was unable to found enough resources out there on the subject. Hope this helps someone.

Scope

What I tried to do was to use PyQt via Eclipse plugin PyDev on Mac OS X 10.7.1 Lion (XCode 4) with Qt SDK 4.7 Static installed on user folder. I also used Python 2.7.2+ from python.org but it wouldn't matter, you could use MacPorts Python, or built-in OS X Python as well.

The Problem

Once I intended to start using PyQt (since C++ Qt is really tough for GUI applications), I already had Qt 4.7 SDK which was actually named Qt_SDK_Mac64_offline_v1_1_2_en installed on my system. This version of Qt, on Mac OS X Lion, installs in User Root (~) folder instead of the /Developer folder. Also its not in the path. My version was also static since Mac OS X philosophy is to live as static as possible, for the sake of simplicity from the end-user PoV. After I downloaded and built SIP on my OS X, I grabbed the source of PyQt 4.8.5 and using default configuration, built that as well.

Though build process was quite tiresome (15 to 30 minutes), Running the program in Eclipse failed with the following error :

ImportError: cannot import name QtCore
ImportError: cannot import name QtGui
ImportError: cannot import name PyQt

You might also face the following error :

Symbol not found: _sipQtConnect

Using python in terminal with vim popped up the same error as well. A few googling did not return anything particular. Since I had access to Ubuntu 11.04 natty as well, I tried the same combination there wihtout any errors and the sample PyQt application worked fine, Though the Qt on Ubuntu was not static (by default). The PyQt Reference Manual on configuring PyQt notes that you should use appropriate --use-arg for your platform/Qt version. You should also use either --static or --consolidate if you're having a static Qt out there. The first switch makes PyQt compile in static mode, which provides you with a bunch of .a files for linking, But since Python is interpreted language, How could you link those against it? I'm sure if there are any solutions to that, They are a real pain in the A. The latter, linkes all Qt libs into one _qt.so and lets other files use that one as the dictionary. But if you follow all these instructions, whether you --use-arg x86_64, i386 or x64, You'll face the second error above when running your PyQt script.

The Solution

You need to set everything right :

  • Let the configure script know where your Qt resides
  • Let the configure script also know which installed Python on your system should be the target
  • Set appropriate architecture
  • Handle static Qt accordingly

So basically you should run the configure script as follows :

python configure.py -q /path/to/qmake -d /path/to/python/site-packages -g --use-arch x86_64

which is the following on my system :

python configure.py -q /Users/abiusx/QtSDK/Desktop/Qt/473/gcc/bin/qmake -d /Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages -g --use-arch x86_64

After the following configuration, you can safely make and make install.
Now you should have your PyQt Shared Object libraries in

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4

with a around 16MB _qt.so file in there as well. Run the code attached to this post to test your newly installed and configured PyQt4 :)

Suggestions?

if you had any problems/suggestions, feel free to contact me.

Leave a comment