If you omit a backend, you must make sure it isn't the default - for example if you package the wx backend and not Tcl/Tk. The default backend is configured in mpl-data/matplotlib.conf. You can override the configuration in your program by doing, for example:

Setup.py using py2exe with Python2.5 and matplotlib 0.91.2

--~~~~ In this example, a simple program was created where a matplotlib figure canvas is placed in a PyQt child window. In order to compile it with py2exe and matplotlib 0.91.2 with Python 2.5, it is necessary to include the necessary modules and then add the data files properly. On this system, the matplotlib was installed to the folder Python25\Lib\site-packages. Within the matplotlib folder, the matplotlib-data is saved in mpl-data.

Using the above methods would result in the classic "RuntimeError: Could not find the matplotlib data files" error. Furthermore, using the method with data_files = matplotlib.get_py2exe_datafiles(), py2exe returns an error saying that 'split' is not a valid method for this object. Another problem when using glob, the and a * is located in the argument, glob will search for everything, including folders. Doing this will give you an error when compiling saying that 'fonts' is not a file. So you need to add the contents from mpl-data\fonts and mpl-data\images individually.

Ensure that the first entry in the tuple in the list 'data_files' is matches the actual matplotlib data folder; in this case 'mpl-data' (and then later mpl-data\fonts and mpl-data\images). Also, the file matplotlibrc file is not returned by 'glob' so it is also added manually.

This is modified from the above setup.py under Special content for setup.py to use matplotlib

1# Used successfully in Python2.5 with matplotlib 0.91.2 and PyQt4 (and Qt 4.3.3) 2fromdistutils.coreimportsetup 3importpy2exe 4 5# We need to import the glob module to search for all files. 6importglob 7 8# We need to exclude matplotlib backends not being used by this executable. You may find 9# that you need different excludes to create a working executable with your chosen backend. 10# We also need to include include various numerix libraries that the other functions call. 11 12opts = { 13'py2exe': { "includes" : ["sip", "PyQt4._qt", "matplotlib.backends", "matplotlib.backends.backend_qt4agg", 14"matplotlib.figure","pylab", "numpy", "matplotlib.numerix.fft", 15"matplotlib.numerix.linear_algebra", "matplotlib.numerix.random_array", 16"matplotlib.backends.backend_tkagg"], 17'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo', '_cocoaagg', 18'_fltkagg', '_gtk', '_gtkcairo', ], 19'dll_excludes': ['libgdk-win32-2.0-0.dll', 20'libgobject-2.0-0.dll'] 21 } 22 } 23 24# Save matplotlib-data to mpl-data ( It is located in the matplotlib\mpl-data 25# folder and the compiled programs will look for it in \mpl-data 26# note: using matplotlib.get_mpldata_info 27data_files = [(r'mpl-data', glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\*.*')), 28# Because matplotlibrc does not have an extension, glob does not find it (at least I think that's why) 29# So add it manually here: 30 (r'mpl-data', [r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']), 31 (r'mpl-data\images',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\images\*.*')), 32 (r'mpl-data\fonts',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\fonts\*.*'))] 33 34# for console program use 'console = [{"script" : "scriptname.py"}] 35setup(windows=[{"script" : "scriptname.py"}], options=opts, data_files=data_files)

Updating _sort confusion

If you're using numarray and numpy together, be aware that they both have a _sort, and py2exe gets them confused. After py2exe creates the dist directotry, copy numpy/core/_sort.pyd and numarray/sort.pyd to their respective locations in the dist.

If you get an error that numarray module has no functionDict in numarraycore.py at line 176, then you'll know that py2exe confused the _sort modules that are in both numpy and numarray and need to do this step.

MPL datafiles etc with Python 2.5 and MPL 0.99

Using matplotlib.get_py2exe_datafiles() helps to make this really easy now.

Following is a setup.py for the MPL example "embedding_in_wx2.py" (you should comment the wxversion check in that file or make it conditional on sys.frozen).

I got this problem only with mpl/mlab.py, in which four lines need to be changed.

Another issue I found if you need to use the backend_wx, it does a version check for wxPython 2.8 which always fails in the py2exe version, my work around is to add "if not hasattr(sys, 'frozen'):" at line 113 of backend_wx.py and indent the lines 114 to 131. I will report this to the mpl list and they will hopefully fix this too.