Let's get started with Python and GObject-Introspection

This week I thought I gave the new shiny GObject-Introspection in Python a chance by porting the UI of GNOME DVB Daemon. First and foremost, I want to mention that I haven’t got too far in the sense of porting the code to PyGI. Why am I writing this post then you may ask? Because it already took me some effort to get the proper build environment running correctly, and that’s what this post is about. I’m running Ubuntu Maverick, so things might work slightly different on other distros. Initially, my plan was to first port PyGI and after that to GTK+ 3.0. I installed the gir packages from the Ubuntu repositories and used the pygi-convert script. After extending the script a little bit I ran into a problem with Gtk.TreeViewColumn.set_cell_data_func and filed bug. There I was told that GObject-Introspection only works properly with GTK+ 3.0. That was new to me, which means I have to port to PyGI and GTK+ 3.0 at the same time. I couldn’t find a PPA with pre-build packages, therefore I used Jhbuild. First of all the following two lines have to be added to .jhbuildrc to make sure that pygobject from git is picked up instead of the system’s one: libdir = os.path.join(prefix, 'lib') addpath('PYTHONPATH', os.path.join(libdir, 'python2.6/site-packages/gtk-2.0')) Make sure you have installed all the dependencies. Now let’s start setting up the build environment by running jhbuild bootstrap. As mentioned above we need GTK+ 3.0, therefore run jhbuild build gtk+-3 Additionally, I had to install docbook-xsl, because gnome-doc-utils failed to build. If you have problems building it might be worth having a look at http://live.gnome.org/JhbuildIssues On my system GTK+ refused to work without libcanberra, therefore I had to install it as well: jhbuild buildone libcanberra Finally, I installed pygobject: jhbuild build --start-at=gobject-introspection pygobject You can run the following command to check if the correct pygobject is used: jhbuild run python -c "import gi.repository print gi.repository.__file__" Which returned /opt/gnome3/lib/python2.6/site-packages/gtk-2.0/gi/repository/__init__.pyc instead of the system’s one: /usr/lib/pymodules/python2.6/gtk-2.0/gi/repository/__init__.pyc Running jhbuild shell will give you a shell where all the paths are set correctly. From within this shell you can run you application and it will use the PyGI stuff from git. Although, I was able to get gnome-control-center running it is far from working correctly. PyGI currently behaves very different from the old PyGTK. In particular, there are no default parameters anymore. In the old times you could do vbox.pack_start(widget) Now you have to provide all parameters every time: vbox.pack_start(widget, True, True, 0) Same applies to callbacks where you always have to provide a parameter for the callback data, even if it’s None. Currently, PyGI behaves much more like the GTK+ C API instead of the good old PyGTK. Especially using ListStore or TreeStore is very un-pythonic. The good news is I’m working on getting this fixed. That said, it seems that the PyGI team could need some more helping hands to get the Python charm back into PyGI. If you are trying to port your application to PyGI please don’t move to the C-style API, but fix PyGI so it behaves like previous versions of PyGTK. The only thing you have to do is override the class you want to change in gi/override/Gtk.py. There you can override methods or add special methods like __iter__.

Sebastian Pölsterl
Post-Doctoral Researcher

My research interests include machine learning for time-to-event analysis, non-Euclidean data, and biomedical applications.