Differences between revisions 21 and 33 (spanning 12 versions)
Revision 21 as of 2008-02-13 17:56:32
Size: 7539
Comment:
Revision 33 as of 2018-10-31 04:23:49
Size: 260
Editor: JohnsonSmi
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Introduction =
[http://matplotlib.sf.net 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.

= Special content for setup.py to use matplotlib =
{{{
#!python
# We need to import the glob module to search for all files.
import glob

# We need to exclude matplotlib backends not being used by this executable. You may find
# that you need different excludes to create a working executable with your chosen backend.
# We also need to include matplotlib.numerix.random_array
opts = {
    'py2exe': { 'includes': 'matplotlib.numerix.random_array',
                'excludes': ['_gtkagg', '_tkagg'],
                'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll']
              }
       }

# Additional data files are required by matplotlib. Note that the glob.glob routine
# doesn't seem to pick up the .matplotlib resource file, so I copy that separately.
# Do the same if you need to
setup(
    data_files = [(r'matplotlibdata', glob.glob(r'c:\python24\share\matplotlib\*')),
                  (r'matplotlibdata', [r'c:\python24\share\matplotlib\.matplotlibrc'])],
    name = 'demo',
    description = 'MatPlotLib Demo Program',
    console = ['demo.py']
    )
}}}

= Copying PyTZ folder =
Now copy pytz folder from C:\Python24\Lib\site-packages to your dist/ folder and you're done!


= Update for newer versions of matplotlib =
Recent versions of matplotlib (this is tested with 0.87) have changed the location where data files are found. Here is a sample setup.py that works with simple matplotlib samples. It produces massive distributions - more work needs to be done on how to exclude unused backends.

{{{
#!python
from distutils.core import setup
import py2exe

from distutils.filelist import findall
import os
import matplotlib
matplotlibdatadir = matplotlib.get_data_path()
matplotlibdata = findall(matplotlibdatadir)
matplotlibdata_files = []
for f in matplotlibdata:
    dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
    matplotlibdata_files.append((os.path.split(dirname)[0], [f]))


setup(
    console=['test.py'],
    options={
             'py2exe': {
                        'packages' : ['matplotlib', 'pytz'],
                       }
            },
    data_files=matplotlibdata_files
)
}}}


= An easier way to get the list of datafiles =
Even more recent version of MatPlotLib(Version 0.90 at least, maybe earlier versions) have a method named matplotlib.get_py2exe_datafiles() which returns a tuple that is ready for use in the data_files argument to distutils.setup(). A minimal setup.py utilizing this method would look something like this:

{{{
#!python
from distutils.core import setup
import py2exe

import matplotlib

setup(
    console=['test.py'],
    options={
             'py2exe': {
                        'packages' : ['matplotlib', 'pytz'],
                       }
            },
    data_files=[matplotlib.get_py2exe_datafiles()]
)
}}}

= 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''
{{{
#!python
# Used successfully in Python2.5 with matplotlib 0.91.2 and PyQt4 (and Qt 4.3.3)
from distutils.core import setup
import py2exe

# We need to import the glob module to search for all files.
import glob

# We need to exclude matplotlib backends not being used by this executable. You may find
# that you need different excludes to create a working executable with your chosen backend.
# We also need to include include various numerix libraries that the other functions call.

opts = {
    'py2exe': { "includes" : ["sip", "PyQt4._qt", "matplotlib.backends", "matplotlib.backends.backend_qt4agg",
                               "matplotlib.figure","pylab", "numpy", "matplotlib.numerix.fft",
                               "matplotlib.numerix.linear_algebra", "matplotlib.numerix.random_array",
                               "matplotlib.backends.backend_tkagg"],
                'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo', '_cocoaagg',
                             '_fltkagg', '_gtk', '_gtkcairo', ],
                'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll']
              }
       }

# Save matplotlib-data to mpl-data ( It is located in the matplotlib\mpl-data
# folder and the compiled programs will look for it in \mpl-data
# note: using matplotlib.get_mpldata_info
data_files = [(r'mpl-data', glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\*.*')),
                    # Because matplotlibrc does not have an extension, glob does not find it (at least I think that's why)
                    # So add it manually here:
                  (r'mpl-data', [r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),
                  (r'mpl-data\images',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\images\*.*')),
                  (r'mpl-data\fonts',glob.glob(r'C:\Python25\Lib\site-packages\matplotlib\mpl-data\fonts\*.*'))]

# for console program use 'console = [{"script" : "scriptname.py"}]
setup(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.
Hello! <<<BR>>
><<<BR>>
>
I'm Portuguese female ;=). <<<BR>>
><<<BR>>
>
I really love Gaming!<<<BR>>
><<<BR>>
>
<<<BR>>
><<<BR>>
>
Here is my blog: reliable locksmith ([[https://jacksonvillelocksmithnow.com|https://jacksonvillelocksmithnow.com]])

Hello! <
><
> I'm Portuguese female ;=). <
><
> I really love Gaming!<
><
> <
><
> Here is my blog: reliable locksmith (https://jacksonvillelocksmithnow.com)

MatPlotLib (last edited 2018-10-31 15:57:34 by JimmyRetzlaff)