9

I'm trying to generate an .exe file from a python script that uses wxPython and Matplotlib and it looks like to be impossible.

The imports I'm doing (related with Matplotlib) are the following:

from numpy import *
import matplotlib
matplotlib.interactive(True)
matplotlib.use("WXAgg")
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
from matplotlib.ticker import MultipleLocator

Here is the setup.py file I'm trying to use:

from distutils.core import setup
import py2exe
import matplotlib

opts = {
'py2exe': {"bundle_files" : 3,
           "includes" : [ "matplotlib", 
            "matplotlib.backends",  
            "matplotlib.backends.backend_wxagg",
                        "numpy", 
                        "matplotlib.ticker",
                        "matplotlib.figure", "_wxagg"],
            'excludes': ['_gtkagg', '_tkagg', '_agg2', 
                        '_cairo', '_cocoaagg',
                        '_fltkagg', '_gtk', '_gtkcairo', ],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                        'libgobject-2.0-0.dll']
          }
   }

setup(


  windows=[{'script':'starHunter.py', 'icon_resources':[(1, 'icon.ico')]}],

  data_files=matplotlib.get_py2exe_datafiles(),

  options=opts,

  zipfile=None
)

I'm always getting "Could not find matplotlib data files" after trying to run the .exe file, which by the way, is successfully created.

Additional information: I'm using Python 2.6, Matplotlib 0.99.3, wxPython 2.8.11.0 on Windows XP

Thanks in advance. Any help will be appreciated!

Cheers, Andressa Sivolella

asivolella
  • 91
  • 1
  • 4

4 Answers4

9

Try using PyInstaller rather than py2exe. It has full support for wxPython and matplotlib. And it's in active development, unlike py2exe.

Velociraptors
  • 2,012
  • 16
  • 22
  • I second this recommendation. PyInstaller works great for wxPython and matplotlib, and a few others not mentioned on their [Supported Packages](http://www.pyinstaller.org/wiki/SupportedPackages) list like [xlrd](http://pypi.python.org/pypi/xlrd). I've been using PyInstaller and all 3 of these packages for the past couple of weeks on a project and it's been just about painless. – ChrisC Aug 23 '11 at 20:13
  • @ChrisC I've also used [cx_freeze](http://cx-freeze.sourceforge.net/), since PyInstaller 1.4 didn't support Python 2.6. I'm a little surprised that py2exe continues to be used so much since it hasn't been updated since 2008. – Velociraptors Aug 24 '11 at 13:54
  • 1
    To be honest, I didn't see much of a difference between py2exe and PyInstaller for a long time and just defaulted to py2exe...until I discovered that PyInstaller handled the MSVCR*.DLL distribution silliness and could wrap matplotlib. Haven't looked back since. – ChrisC Aug 24 '11 at 15:24
  • You can definitely deliver a package with matplotlib using py2exe; see otterb's answer. – Chelonian Feb 21 '12 at 19:57
  • pyinstaller is much better than py2exe! Thanks for the hint! – Nicholas Aug 03 '14 at 12:47
1

There are a number of problems with matplotlib.get_py2exe_datafiles(), as convenient as it would be if it worked. It's also a good idea to specify which backend to use. Here's a working matplotlib import I recently used:

from distutils.core import setup
import py2exe
from glob import glob

import matplotlib       #Import then use get_py2exe_datafiles() to collect numpy datafiles.
matplotlib.use('wxagg') #Specify matplotlib backend. tkagg must still be included else error is thrown.

data_files = [
            ("Stuff", glob(r'C:\ProjectFolder\Stuff\*.*')) 
            ,("dlls", glob(r'C:\ProjectFolder\dlls\*.dll'))  
            ,("pyds", glob(r'C:\ProjectFolder\pyds\*.pyd')) # py2exe specified pyd's 
            ]
# Extend the tuple list because matplotlib returns a tuple list.      
data_files.extend(matplotlib.get_py2exe_datafiles())  #Matplotlib - pulls it's own files

options =   {'py2exe':{#'bundle_files': 1,                                 # Bundle files to exe
                        'includes': ["matplotlib.backends.backend_tkagg"]  # Specifically include missing modules
                        ,'excludes': ['_gtkagg', 'tkagg']                  # Exclude dependencies. Reduce size.
                      }
            }   

setup(
name='ProjectName'
,options = options  
,data_files=data_files
,console=['projectname.py']
)
Kirk
  • 144
  • 10
  • 1
    Thank you so much. I know this answer is super old, but I was thinking I would never ever ever be able to successfully convert my program to an exe. I know, "THANK YOU" isn't technically a valid comment but this really really really helped me. – mauve Aug 29 '18 at 18:38
1

For a simply test, you could simply copy 'mpl-data' folder in 'site-packages\matplotlib' to your app folder. As far as I know, 'mpl-data' cannot be bundled into the single executable so this has to be included in your binary distribution as a folder.

I used py2exe via GUI2Exe and could freeze my app that uses matplotlib + numpy/scipy + wx (so obviously wxagg backend). I didn't need to include _tkagg (which is explicitly excluded in GUI2Exe default setting which worked for me).

otterb
  • 2,660
  • 2
  • 29
  • 48
0

Py2exe documentation explains the source of the problem and give solutions. It worked for me. (matplotlib version 1.1.0, Python 2.7)

http://www.py2exe.org/index.cgi/MatPlotLib

Since I have no privilege to comment or evaluate other answers, I have to write my own one. Kirk's answer was the most valuable help for me. PyInstaller might be a workaround (have not tested it) but is definitely not the technical solution to the problem !

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': {
                        'includes': ["sip", "PyQt4.QtGui"],
                        'packages' : ['matplotlib', 'pytz'],
                        'excludes': ['_gtkagg', '_tkagg']
                       }
            },
    data_files=matplotlibdata_files
)
Rv F
  • 1