py2exe wisdom

For the record, here is what I'm using to wrap matplotlib in py2exe.

Since I'm using the wx backend I've excluded Tkinter.

Since numerix uses __import__, py2exe wasn't clever enough to find the
numerix extensions, and had to be listed explicitly. Similarly
for backends.

I'm not picking up the 14Mb of timezone info that I don't need. Maybe
want packages = ['pytz'] if your applications need it.

I haven't addressed mpl-data yet.

  - Paul


On Tue, Oct 02, 2007 at 10:42:13AM -0400, Paul Kienzle wrote:

As I was building a py2exe distribution of matplotlib, I noticed the
function get_py2exe_datafiles() in that is not noted on
the FAQ. Before I update the FAQ, can you all tell me your best
practices recommendations for wrapping matplotlib?

In particular, is there a way I can store the matplotlib data directly
in the exe so that install is simply a matter of copying an exe file
rather than a whole directory?

Technically this should be feasible --- freetype can load fonts
from memory or directly from the zip file given the proper driver,
and the various images should be similarly readable.

import pylab

from distutils.core import setup
import sys, os, py2exe

# data_files: List of datafiles that need to be explicitly included
# e.g., [('target/relative/path', ['file','list'])]
# excludes: List of modules to exclude (takes precedence over includes)
# dll_excludes: List of dlls to exclude
# includes: List of modules to include
# packages: List of packages to include
data_files = []
excludes = [ 'numarray', 'Numeric', 'Tkinter', ]
dll_excludes = [] # dlls to exclude
includes = [ 'matplotlib', 'wx', 'numpy', ]
packages = [] # May want 'wx' in packages if creating a generic environment

# Explicit package rules
if 'matplotlib' in includes:
    import matplotlib
    data_files += matplotlib.get_py2exe_datafiles()
    includes += ['matplotlib.numerix.'+pkg for pkg in
    includes += ['matplotlib.backends.backend_'+pkg for pkg in

# Don't include anything explicitly excluded
includes = [x for x in includes if x not in excludes]

# Include the C/C++ runtime (there should be a way of putting these beside
# the python dll in the executable, but for now I put them in the executable
# directory. Note: the C++ runtime might be in the wx package instead of the
# python directory.
pythonpath = os.path.dirname(sys.executable)
c_runtime = pythonpath+"/MSVCR71.DLL"
cpp_runtime = pythonpath+"/MSVCP71.DLL"
data_files += [('.',[c_runtime,cpp_runtime])]

# End of configuration

py2exe_opts = dict(includes=includes, excludes=excludes,
                   skip_archive=0, compressed=1,
                   dll_excludes=dll_excludes, packages=packages,
                   bundle_files=1, optimize=2)
    options = dict(py2exe=py2exe_opts),
    windows= [''], # Could be console=, depending on your app
    data_files = data_files,
    #zipfile = None, # Bundle with the executable