Possibility and Probability

A Python programmer with a personality thinking about space exploration

29 May 2010

Linking against Python on the Mac

by nickadmin

Today while working with swig I kept running into problems trying to get python to read the generated output files. The error I finally wound up with was:

Fatal Python error: Interpreter not initialized (version mismatch?)

Abort trap

Fatal Python error: Interpreter not initialized (version mismatch?)

Abort trap

It turns out that on the Mac (OSX 10.6) that you have to pass gcc a special flag if you want to link against the python library. Traditionally you would do something like this:

gcc myfile.c -l python -shared -o mylib.so

But on the Mac is you have the Mac specific installer version of python (I think as opposed to building it at the command line), there is no libpython.so on your system, so the -lpython flag never works.

The solution is to use the -framework which tells gcc to look into the framework directory where OSX stores all of its specially configured tools/utilities/programs. (If you are wondering the python 2.6 framework is kept at this location: /Library/Frameworks/Python.framework/Versions/2.6/)

So using the example above, the proper incantation to produce a useable shared library linked against python is:

gcc myfile.c - framework python -shared -o mylib.so

That should produce something useable.