Learning Python and the Mac OS X Cocoa Framework:

The struggles of a reluctant scientist to learn a useful language with a decent interface

W. G. Scott
nice icon


A few useful links


Python home pages

Apple's Cocoa Developer pages

The Python Objective C project home page


Fink allows automated installation and updates of python and loads of other unix opensource and GNU free software

ScientificPython modules

Numeric Python modules



Learning Cocoa 2nd ed
This is better than the
first edition.


This is just better.


There are several python books
available from O'Reilly and other publishers.  I still haven't found
one that I really like.



My main scientific computational interest is in X-ray crystallographic structural studies of biological macromolecules.  Here is my web page called crystallography on OS X.

Introductory Comments


Python
is a scripting or interpreting language that is quite powerful and has lucid syntax, which means it is less painful to read and learn than fortran.

Cocoa is the the full fledged Mac OS X framework that one uses to create OS X native applications.  Its preferred language is Objective C, which is said to be a superset of C, and from my point of view appears to be even more obtuse than C. However, bridges to Java and Applescript are also available for writing Cocoa applications, and now, thanks to the PyObjC project, Python is now available as well.

PyObjC creates a bridge between Cocoa and Python, thus in principle makes available the myriad of python modules to Cocoa programmers on OS X.  From my point of view, I am hoping it will allow me to jump-start the process of writing useful scientific applications, or at least enable me to stitch together other people's useful scientific applications.




Getting Started


Download the pyobjc disk image and install it as you would any normal OS X application.  This automatically installs the relevant files in the Developer tools directory as well as several subdirectories in the OS X v. 10.2.x system python under /usr/lib/python2.2/site-packages/    This is fine if you only need to make use of the system-supplied python.  However if you have used fink to install large numbers of site-packages like I have, you may want to do the following:


How to Use Python installed by Fink with PyObjC:

1.  Change the line /usr/bin/python to /sw/bin/python in the supplied template file /Developer/ProjectBuilder Extras/Project Templates/Application/Cocoa-Python Application/bin-python-main.m

2.  Make the AddressBook/ AppKit/  Foundation/  and   objc/  directories available to your fink python installation by doing any one of the following:

a.  cp -R /usr/lib/python2.2/site-packages/* /sw/lib/python2.2/site-packages/

b.  symbolically link /usr/lib/python2.2/site-packages to /sw/lib/python2.2/site-packages  prior to installing pyobjc

c. symbolically link each of the four subdirectories under /usr/lib/python2.2/site-packages/ to the corresponding files under /sw/lib/python2.2/site-packages/  after installing pyobjc

d.  Install the source (obtained from cvs) to pyobjc in /sw/lib/python2.2/site-packages/ manually using   sudo /sw/bin/python setup.py install



Links to Some Worked Examples


These will appear as additional pages as I work through them. My initial plan (subject to revision) is to follow along the lines of Building Cocoa Applications, but to write the code in Python.  Then if that works I will try to get more ambitious.  It would be great if people who, unlike me, actually know what they are doing, critique this along the way.  My hope is that in addition to enjoying the spectacle of self-inflicted public embarrassment and humiliation, the reader might find this to be of some use, at least until someone writes a book (hint).







Click here for web site index