matplotlib [] is a module to produce nice-looking plots in Python using a wide variety of back-end packages, at least one of which is likely to be available for your system. This ability to do things in a generic fashion makes this a simple system to use, but it gets complicated if you wish to distribute an executable instead of scripts. This page describes what I had to do to make matplotlib work with py2exe.

Changes required to matplotlib

I had to patch matplotlib's file in the get_data_path() routine. Specifically, right before raising the error at the end, I added the following code

   2       if sys.frozen:
   3          path = os.path.join(os.path.split(sys.path[0])[0], 'matplotlib')
   4          return path

Special content for to use matplotlib

We also need to make sure we get the matplotlib subdirectory created in the distribution. This is where we'll include all of the stuff matplotlib includes in the pythonXX\share\matplotlib directory. This is also where the init file has been updated to look for this information.

   1 # We need to import the glob module to search for all files.
   2 import glob
   4 # We need to exclude matplotlib backends not being used by this executable.  You may find
   5 # that you need different excludes to create a working executable with your chosen backend.
   6 opts = {
   7     'py2exe': { 'excludes': ['_gtkagg', '_tkagg'],
   8                 'dll_excludes': ['libgdk-win32-2.0-0.dll',
   9                                  'libgobject-2.0-0.dll']}
  11 # Additional data files are required by matplotlib.  Note that the glob.glob routine
  12 # doesn't seem to pick up the .matplotlib resource file, so I copy that separately.
  13 setup(
  14     data_files = [('matplotlib', glob.glob(r'c:\python23\share\matplotlib\*')),
  15                   ('matplotlib', [r'c:\python23\share\matplotlib\.matplotlibrc'])],
  16     name = 'demo',
  17     description = 'MatPlotLib Demo Program',
  18     console = ['',]
  19     )