How to setup pyobjc with enthought python

In my first blog post ever, I will describe how I got pyobjc running using the enthought python distribution.  For a while I am trying to learn Cocoa programming but I find coding in C (or objective C for that matter) very unproductive.  Coding in C just takes forever.  My favorite programming language for quite some time now is python.  It is a wonderful language and ideally suited for my needs especially with the numpy, scipy and matplotlib modules.

So recently, I have been trying pyobjc again.  I had given up several years back because it was just too hard to get everything running. pyobjc is a Cocoa-python bridge that should allow you to code Cocoa in python.  Unfortunately, pyobjc has almost no documentation.

So here is how you do it

1. Install XCode (I have Version 3.2)

2. Download and install enthought python (I installed EDP 7.0-2)

2. Open the terminal application and type:

sudo easy_install -U setuptools

sudo easy_install virtualenv

create a directory for virtual environment.  All my virtual environment live in “/Users/hstrey/programming”.

virtualenv pyobjc23

cd pyobjc23

source bin/activate

easy_install pyobjc-core

easy_install pyobjc

virtualenv establishes a safe installation that does not interfere with the standard installation. Every time you want to use pyobjc go to your virtualenv folder and activate it by source bin/activate

Now we need to install the XCode pyobjc templates.  Mount http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/ using “Connect to server” from the Finder.  Copy the “File Templates” folder into “/Library/Application Support/Developer/Shared/Xcode” (create this path if necessary). Copy “Project Templates” to the Desktop and follow the instructions in the README.txt file to install everything.

Open Xcode and create a new XCode project.  Under User Templates/AA Testing you will find the pyobjc templates.  Choose Cocoa-Python Application.  Call the project test for right now.

Open main.py.  Add the following two lines before the import statements:

activate_this = '/Users/hstrey/programming/pyobjc23/bin/activate_this.py'

execfile(activate_this, dict(__file__=activate_this))

Before compiling the project we need to replace the Python framework that is used for linking with the enthought python framework.  Right-click on Python.framework and Show Info.  Now change the path to “/Library/Frameworks/Python.Framework”.  We also need to adjust the project settings to adjust for the fact that enthought python is intel-only and 32-bit (at least my academic version is).  Go into Project – Edit Project Settings and select the Build tab.  Here delete all valid architectures except for i386 and select Build Active Architecture only.

Now you can click on Build and run and voila – the program should compile and an empty window will appear.  Your first pyobjc application is running!

Later in this series, I will show you how to write most of applications in “Cocoa programming for Mac OS X” by Aaron Hillegass using pyobjc.  And it is so much simpler than Objective C.

Posted in Macintosh programing, pyobjc, python | Leave a comment