Py2exe with PyOpenGL and OpenGLContext

py2exe is a distutils extension that allows you to build "stand-alone" executable versions of your applications for Win32 systems.  Because of the structure of the OpenGL package, it's necessary to take a few extra steps to get a functional py2exe-generated application when using PyOpenGL. OpenGLContext uses a common pattern of "lazy imports" for certain classes, so those require a simple flag.


The information on this page is apparently out-of-date.  It was written for earlier versions of py2exe that weren't able to deal with PyOpenGL's funky import hooks.  Reports are that there is now nothing special required to use PyOpenGL 2.0.2 in py2exe'd applications.

The OpenGL package uses a number of import tricks to make it convenient to use the packages in the normal case.  However, these import tricks (specifically the use of __init__.pyd or files) are not compatible with the py2exe import hooks (which get confused as to which  __init__.pyd is intended).  To get around this limitation, you can exclude the entire OpenGL package during py2exe building, then copy the package as a directory into your distribution target directory.

The OpenGLContext testingcontext package (if used) doesn't directly import the various testing context modules, so if you use it, you'll need to import those modules explicitly.

PyOpenGL Details

Use the --excludes=OpenGL flag to py2exe, like so: py2exe --excludes=OpenGL

this prevents the OpenGL package, or any sub-module from being included.  This avoids the conflicts with the various __init__.* files.

Import Tkinter directly in your script if you use OpenGL.Tk (many scripts don't explicitly import Tkinter, they just use the environment from OpenGL.Tk, which will be excluded by the step above, so Tkinter won't get packaged up).  Alternately use: py2exe --includes=Tkinter ...

flag for py2exe 

Copy (a subset of) the OpenGL package into your distribution directory

You can download a sample archive with 3 of the PyOpenGL demos configured for py2exe use, including one fully py2exe-packaged demo.

OpenGLContext Details

For OpenGLContext (beyond the OpenGL requirements above), you need to explicitly import/include the specific Context sub-class you want to use in your application.  For most real-world applications, this will already be done (since you normally only use a single context type for a given application), but testing or trivial scripts will often be using the testingcontext module to allow the user to specify the context to be used.  This is the same type of inclusion you would do for PIL or any other late/lazy/plug-in binding system.

For example: py2exe --excludes=OpenGL --includes=OpenGLContext.wxtestingcontext

to include just the wxPython testing context for use by your script.