Gtk+ and Python

Overview

Gtk+ has two sets of bindings for Python: PyGtk and PyGI (which is short for Python GObject-Introspection). The former is a statically-linked Python binding for Gtk+-2; the latter is based on GObject-Introspection which enables binding directly to libgtk without needing a statically-built intermediate library. GObject-Introspection is available for Gtk+-2, so you can migrate your application in two steps, first converting to PyGI then to Gtk+-3. Do note, however, that the current releases of gobject-introspection don't successfully scan Gtk+-2 (meta-gtk-osx-core builds an older version that can scan Gtk+-2.24.4), so plan to build separate Gtk+-2 and Gtk+-3 trees.

Building

To build PyGtk, just do

jhbuild build meta-gtk-osx-python

after you've built bootstrap, meta-gtk-osx-bootstrap. (meta-gtk-osx-python includes meta-gtk-osx-core as a dependency so you needn't list it in your modulelist.) If you want to build Python in your tree (either because you need a module that isn't included in the Apple-provided Python, want a newer version of Python than came with OSX (the module builds 2.7.1), or are bundling your application (see below)), add python and any other optional modules like berkeleydb to your modules array or to the list on the command line. There are some additional considerations on Snow Leopard and Lion, do read that page if you're using it.

If you're building a Gtk+-3 based Python application, you want meta-gtk-osx-python-gtk3 instead of meta-gtk-osx-python. That will pull in the meta-gtk-osx-gtk3 moduleset for you.

Also, if you're working with a program that had its interface designed with a version of Glade earlier than 3.6, you'll need to explicitly build libglade, which is no longer automatically pulled in by pygtk.

Bundling

Please read Bundling for the details of creating an application bundle.

The way Apple does relocatable linking, which is needed for application bundles, sets the link path from the binary executable. For a Python program, that's python, and that means that python needs to be in Your.app/Contents/MacOS/. If the bundle is going to work on more than one version of OS X, the associated libraries for that version of Python need to be in the bundle as well. It's possible to set up gtk-mac-bundler to pull together a package from an Apple-installed python, but it's much easier to just build Python in your Gtk tree: Just add "Python" to the list of modules, as noted above.

The launcher shell script will need to set the additional variables $PYTHON, $PYTHONHOME, and $PYTHONPATH to point to the appropriate places in the bundle to ensure that all of the right python parts are used.

See the gtk-mac-bundler pygtk-demo bundle for an example of how to include Python in your bundle.

More

Add your own tips and instructions below:

Some gradually-evolving instructions here (Dec 2009): * http://ascendwiki.cheme.cmu.edu/Porting_to_Mac

Projects/GTK+/OSX/Python (last edited 2013-11-22 15:36:59 by WilliamJonMcCann)