Tuesday, November 27, 2012

Python 3 and PyObjC

My main programming platform is OS X 10.8 and Python 3 (currently Python 3.3). In earlier blog posts I have discussed installing and using PyCrypto in this environment. My next attempt was to find a GUI solutions for this platform. I usually prefer native approaches when programming, and for OS X the Cocoa framework is the native approach. To get access to this from Python we need a bridge, and the bridge is called PyObjC. At the home page PyObjC seems like a dead project. But Ronald Oussoren is still doing some development, including Python 3 support. It is not straight forward to compile and install PyObjC for Python 3, but Giacomo Lacava provides the necessary steps. To summarize I have done the installation using the following steps:

  1. Fetch the latest code from Ronald Oussoren:
    hg clone https://bitbucket.org/ronaldoussoren/pyobjc
    cd pyobjc
  2. Compile and install PyObjC core:
    (cd pyobjc-core && python3 setup.py install)
  3. Compile and install the Cocoa, CoreData and Quartz frameworks:
    for i in Cocoa Quartz CoreData; do
      (cd pyobjc-framework-$i && python3 setup.py install)
  4. Compile and install the rest of PyObjC (except the XgridFoundation framework):
    for i in pyobjc-framework-[^X]*; do
      (cd $i && python3 setup.py install)

So far everything seems to work OK. You need Mercurial (hg) to perform the first step.


(aa) said...

I would change the first line of step 4 to for i in pyobjc-framework-[^X]*; do to prevent to try to build pyobjc-framework-XgridFoundation that will fail (and you might not need).

(aa) said...

This seems to work OK with Xcode 5.1 installed on OS X 10.9.2.

(aa) said...

See this nice example of a OS X Cocoa Application using Python and Interface Builder. It works with Python 3.

lex said...

any help on getting the bindings to work when trying to compile from OS X 10.10 beta 1?

Running into a lot of only for OS X version >= 10.6

and this when trying to build cocoa:
Installed /usr/local/lib/python3.4/site-packages/pyobjc_framework_CoreData-3.1-py3.4.egg
Processing dependencies for pyobjc-framework-CoreData==3.1
Searching for pyobjc-framework-Cocoa>=3.1
Reading https://pypi.python.org/simple/pyobjc-framework-Cocoa/
No local packages or download links found for pyobjc-framework-Cocoa>=3.1
error: Could not find suitable distribution for Requirement.parse('pyobjc-framework-Cocoa>=3.1')

also when running python3 from the terminal and trying to import cocoa it fails