using py2exe with wxPython and Matplotlib

只愿长相守 提交于 2019-12-03 00:12:58

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

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']
)

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).

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
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!