February 13 2012

Generate python (using pygtk) executables using py2exe (win32)

I’m getting old…real old…now I need to write everything down in case they somehow slip my mind…
I’ve been trying to generate executables for my little program written in Python, under both x64 and Linux(Ubuntu 11.04), and with various programs…but I haven’t been really successful, despite the fact that I might not have put in enough effort as well… As a matter of fact, none of my executable really works on others’ machine. (works perfect on my own PC though)
Anyway, now there is something which seems to be working…the environment is x86 this time…with the help of py2exe
So again, I’m getting old, and I’m writing it down…
For record, the following is basically a combination of several recipes found on the web, I don’t really have any personal contribution to this…
So, py2exe should normally work with simple programs, a ‘hello world’ for example…but things just get complicated once PyGtk and Matplotlib are involved…
Let’s have a look at my setup.py first…

from distutils.core import setup
import py2exe
import matplotlib

    name = 'PyRod',
    version = '1.5',

    windows = [
                      'script': 'PyRod.py',
                      'icon_resources': [(1, "Rod of Asclepius.ico")],

    options = {
                  'py2exe': {
                      'includes': 'cairo, pango, pangocairo, atk, gobject, gio',
                      'dll_excludes': ['libglade-2.0-0.dll'],

    data_files = matplotlib.get_py2exe_datafiles(),

1. You should manually include ‘cairo’, ‘gobject’, ‘gio’ etc.
2. You should add ‘matplotlib.backends.backend_tkagg’ to ‘includes’ if you do not specify a different default backend other than tkagg by matplotlib.use() in your program.
3. You could manually exclude ‘libglade-2.0-0.dll’.
4. You should provide the necessary data files for matplotlib by data_files = matplotlib.get_py2exe_datafiles().
5. Copy msvcp90.dll to your program folder (This is actually inappropriate as one does not normally has the right to redistribute this file). The correct solution might be a little bit more complicated.

One more thing, if one is to use other theme engines than the default ones provided in standard Gtk installation.
One should first download and precompiled engines and associated themes here (or you’ll have to get the source code and compile them yourself)
Remember to copy the /etc /share and /lib directories (or at least that of the engines that are required) from your Gtk installation folder to the distribution folder generated by py2exe.
Check out this post if you don’t know how to tell GTK on Windows which theme to use.

I curse x64.

Tags: , ,

Posted 13/02/2012 by Tao in category "Uncategorized

Leave a Reply