How to Install PyQt4 on OS X

08.09.2013
6625 Okunma

res-py2app-pyqtHello! In my tutorial i’ll describe how to install PyQt4 on Os X manually. You can try installing by MacPorts or HomeBrew. I had tried both. But i couldn’t get the result what i wanted. So i managed my goal by installing and configuring manually.

This is a tutorial on Mac OS X 10.8.4, with: Python2.7; with : PyQt-mac-gpl-4.10.4-snapshot-f32540dbcf98.tar.gz and with : sip-4.15.2-snapshot-14732b487dda.tar.gz.

It should be the same on previous and next versions.

How to install PyQt4 ?

Download the SIP package (snapshot for OS x )
Download the PyQt package (snapshot for OS x)
Download Qt
Install Qt
Install SIP
Install PyQt
and try !

Download the Qt binary from the website : Download site (You may take the complete SDK) or Download here.

Download the SIP snapshot : Download site (for OS x)

Download the PyQt snapshot : Download site (for OS x)

1. And now install the first Qt binary, just follow and click on next until it as finished to install.
2. SIP :

SIP is the program that bind C++ with Python

Go were you have downloaded the sip tar.gz and enter the folder(Mostly Downloads folder)

cd ~/Downloads/sip-4.15.2-snapshot-14732b487dda

And we are gonna try to install it with the following lines :

python configure.py -d /Library/Python/2.7/site-packages --arch x86_64

– arch x86_64 is to specify that we want this architecture. You may use i386 also!

Then lets make it and install it with the following commands :

make
sudo make install

3. PyQt4:

Once installed let’s move back an go to the PyQt folder to configure it and install it with the following command :

cd ..
cd PyQt-mac-gpl-4.10.4-snapshot-f32540dbcf98

And let’s try to configure it with the right things

python configure.py -q /opt/local/bin/qmake -d /Library/Python/2.7/site-packages/ --use-arch x86_64

Important note: I had trouble finding qmake! If the path above doesn’t work for you, first try this command if you had qmake or not

qmake -v

After pressing return if you got the version that means you have qmake. Then type:

which qmake

After pressing return you will get your qmake path.
If you don’t have qmake that means you didn’t install Qt. So install it!

Then let’s make them with:

make            #that may long a few minutes
sudo make install

And now, everything should be Up and running. So let’s test it. On a terminal window type

python

When you are on interactive shell type:

import PyQt4

After pressing return, if you dont get a error that means you have installed PyQt. you are now good to go.

4. Hello World by PyQt

# Allow access to command-line arguments
import sys

# SIP allows us to select the API we wish to use
import sip

# use the more modern PyQt API (not enabled by default in Python 2.x);
# must precede importing any module that provides the API specified
sip.setapi('QDate', 2)
sip.setapi('QDateTime', 2)
sip.setapi('QString', 2)
sip.setapi('QTextStream', 2)
sip.setapi('QTime', 2)
sip.setapi('QUrl', 2)
sip.setapi('QVariant', 2)

# Import all of Qt
from PyQt4.Qt import *

# Every Qt application must have one and only one QApplication object;
# it receives the command line arguments passed to the script, as they
# can be used to customize the application's appearance and behavior
qt_app = QApplication(sys.argv)

# Create a label widget with our text
label = QLabel('Hello, world!')

# Show it as a standalone widget
label.show()

# Run the application's event loop
qt_app.exec_()

Save the python code above (hellow.py) and execute in terminal:

python hellow.py

 

Share Button

Comments of this post

Henüz yorum bulunmuyor!