Category Archives: Python

Minimalistic OpenGL setup as PyQt script

Just added a minimalistic setup for PyQt apps using OpenGL. It’s quite compact and it appears as one file that can be plundered here. MIT license as usual.

Keeping the Cmd-key pressed while using the mouse will allow changing the pivot’s position while keeping the Alt-key down will allow rotating the scene. The whole setup runs at 60 FPS (20 msec interval) but can be customised.


How to switch between multiple MacOS X Python framework versions

This is mainly a note-for-self kinda post.

I’ve got multiple Python framework versions installed in my Mac, including MacPorts’ versions.

If I need to switch between MacPorts versions, I can use the usual

sudo port select python python26
sudo port select python python27

but if I need to switch to a MacPython version then I have to update the PATH manually like this.

export PATH

At this point, I can type   which python  in the shell and check whether the path has been changed.

To revert back to the MacPorts version, I will use

export PATH

Again, you can double-check by typing   which python  in the shell.

Now, what I’ve noticed is that at this point the

sudo port select python python27

won’t take effect anymore. I mean after the PATH has been manually changed, MacPorts above command doesn’t seem to be any longer effective although it said it succeeds in doing the selection. That means that manually changing the PATH may have compromised the port selection functionality in some ways, please let me know if you’ve got a clue of what might’ve happened to it.

That’s all. Err, not yet. I found this blog post very helpful.

Installing py2app for Python 2.6.8 (MacPorts) on Mac OS Lion

I needed to install py2app in my main python (2.6.8) which was installed a while ago through MacPorts and has all the useful libraries already in it, PyQt, openCV, numpy, etc.

I tried installing it with the usual :

sudo easy_install -U py2app

but it just installed py2app in the Apple’s Python 2.7 and then skipping my main Python 2.6.8.

I then tried to go to to download the egg file for Python 2.6 setuptools-0.6c11-py2.6.egg and then do the

sh setuptools-0.6c11-py2.6.egg

from Terminal but it didn’t work either as it threw the error “…no matching architecture in universal wrapper” as Python 2.6.8 is a 64bit version to be able to work with openCV and the egg was presumably 32 bit.

So, I had to download the source code and install it manually, see below for the Terminal commands:

cd Downloads
tar xfz setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python build
sudo python install

at this point to install py2app in the right python (2.6.8), all I needed to do was to choose the right path to the bin to be able to use the newly installed easy_install.

sudo /opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/easy_install -U py2app

that’s it. You can try it out by typing python (2.6.8) in the Terminal

import easy_install
import py2app
import py2applet

and it shouldn’t throw any error at this point.


Two additional commands are required to get a python app standalone.

sudo /opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/py2applet --make-setup /Users/macbookpro/git/JADE/src/

this one produced a file in the home folder (mine was /Users/macbookpro/, and at last

sudo python /Users/macbookpro/ py2app -A

creates the standalone in the dist folder, always in the home folder.

installing OpenCV on MacOS Snow Leopard for Python 2.6

As a result of encountering problems in installing OpenCV, I thought I would drop a few lines with the sequence of steps that worked for me, in the end. However Homebrew looks cooler, and probably it is, I found it more helpful to make use of MacPorts and do all of the installation with it.

My specific problem was that Python 2.6 (Framework) was 32-bit which then clashed with the Homebrew installation of OpenCV 2.3.1a which is 64-bit. Homebrew instructions says you can customise the installation to 32-bit but… it just didn’t work, not even in installing Python 2.6 64-bit as a Framework.

So, I moved on to try the MacPorts option and that actually managed to install Python 2.6 64-bit as a Framework, in the first place, and the rest of the libraries followed smoothly after too.

So, installing MacPorts is step one. Use the Terminal from now on.

2) sudo port selfupdate
3) install zlib, sudo port install zlib +universal
4) install Python 2.6.8 (as framework and 64-bit), sudo port install python26 +universal
5) make it the first choice,
sudo port select –set python python26 (or ‘sudo port select python python26’ in Mac OS Lion)
6) install OpenCV,
sudo port -v install opencv +python26
7) (optional) install PyQt4 and OpenGL library,
sudo port install py26-pyqt4

sudo port install py26-opengl

That’s all.

A test to see whether your python is 64-bit is open python in Terminal and type
print hash(‘a’)
If the result is 12416037344 then you’re setup with a 64-bit python framework.

Hope this helps,