Search code examples
pythonpython-3.xpy2exepygobject

Cannot Include gi.repository.Gtk in py2exe


I have a project in Python 3.4 and GTK+ 3. I'm on Windows XP SP3 32-bit (VirtualBox).

I need to compile down to an executable using py2exe. (Do NOT suggest cx_freeze. It has ten times the problems on this project than py2exe).

My setup.py is as follows.

#!/usr/bin/python

from setuptools import setup
import py2exe

setup(name="Redstring",
    version="2.0",
    description="REDundant STRING generator",
    author="MousePaw Labs",
    url="http://www.mousepawgames.com/",
    maintainer_email="[email protected]",
    data_files=[("", ["redstring.png", "redstring_interface.glade"])],
    py_modules=["redstring"],
    windows=[{'script':'redstring.py'}],
    options={"py2exe":{
        "unbuffered": True,
        "compressed":True,
        "bundle_files": 1,
        'packages':['gi.repository'],
        }},
    zipfile=None
    )

When I run it via C:\Documents and Settings\Jason\Desktop\redstring2>python setup.py py2exe, I get the following output (in full).

running py2exe
running build_py

  1 missing Modules
  ------------------
? gi.repository.Gtk                   imported from __SCRIPT__
Building 'dist\redstring.exe'.

C:\Documents and Settings\Jason\Desktop\redstring2>

The actual script, redstring.py, runs without a hitch in my Windows environment. In that, I have the following (working) line of code: from gi.repository import Gtk That is ALL I import from gi.repository in the entire project.

If I swap the line in setup.py to 'packages':['gi'],, the error output switches to about 24-some-odd missing modules, all of them belonging to gi.repository. If I try and import "Gtk" or "gi.repository.Gtk" in either 'packages': or 'includes':, I get an error that the file in question being imported cannot be found.

I spent eight hours on #python (IRC channel) today, and no one could solve this. I need this packaged down to a Windows binary this week.

NOTE: This question is not a duplicate; while it is a similar issue, it is a) not the same error message, and b) neither answer solves the question in any way.


Solution

  • I solved this by, first of all, downgrading to Python 2.7. (GTK+ 3.8 is still fine.) py2exe apparently has known issues with Python 3.

    Second, I switched...

    options={"py2exe": {
        "bundle_files": 1, 
    }
    

    to

    options={"py2exe": {
        "bundle_files": 3, 
    }
    

    For some reason, py2exe cannot include certain files needed to run the gi library when 'bundle_files' is set to 1 or 2.

    The full setup.py that works with py2exe for my project can be found on GitHub. I run it on cmd with python setup.py py2exe.