Fixing the PyAverager sample on ADC

A few days ago I ran across this old sample on ADC: Using PyObjC for Developing Cocoa Applications with Python. PyObjC is the Python/Objective-C bridge. Similiarly, there is also a Ruby bridge, RubyCocoa. Personally, I find the possibility of using Python or Ruby to write Cocoa applications more appealing than using Objective-C.

In any case, the example is pretty straight forward and even has helpful (necessary?) videos for the more involved steps. After completing everything, however, you’ll find that it will not build:

Compile error

The build tool is complaining about not finding usr/bin/env python. Apparantly, the PyObjC Cocoa template for setup.py is geared more towards looking for Python in a traditional Unix environment. This is an easy fix though.

Simply run Terminal and type in which python to find the correct path to Python on your Mac OS X system:

Terminal

Copy-and-paste your python path to the top line of the setup.py file while keeping the first two characters:

Paths

After saving your changes, re-building and running your app, you’ll notice that you are now presented with a runtime error:

Runtime error

This requires another simple fix. You’ll have to add an import line to the PyAverager.py file. The new line is highlighted below:

# 
# PyAverager.py
# PyAverager
#

from PyObjCTools import NibClassBuilder, AppHelper

info = NSBundle.mainBundle().infoDictionary()[u'PyObjCXcode']

for nibFile in info[u'NIBFiles']:
    NibClassBuilder.extractClasses(nibFile)

for pythonModule in info[u'Modules']:
    __import__(pythonModule)

__import__('Averager')

if __name__ == '__main__':
    AppHelper.runEventLoop()

Another round of saving/re-building and you’ll now be able to run the sample as expected:

Running PyAverager

Sliding Sidebar