2011-06-27 51 views
8

我试图从使用wxPython和Matplotlib的python脚本生成一个.exe文件,它看起来像是不可能的。使用py2exe与wxPython和Matplotlib

我做的是进口的(含Matplotlib相关)如下:

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

这里是我试图使用setup.py文件:

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 
) 

我试图运行.exe文件(顺便说一句,它已成功创建)后,总是收到“找不到matplotlib数据文件”。

更多信息:我使用Python 2.6,Matplotlib 0.99.3,wxPython的2.8.11.0在Windows XP

在此先感谢。 任何帮助将不胜感激!

干杯, Andressa Sivolella

回答

0

有许多的问题matplotlib.get_py2exe_datafiles(),因为方便,因为这将是如果它的工作。指定使用哪个后端也是一个好主意。这里有一个工作matplotlib进口我最近使用:

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

尝试使用PyInstaller而不是py2exe。它完全支持wxPython和matplotlib。与py2exe不同,它处于积极的发展阶段。

+0

我第二个这个建议。 PyInstaller非常适用于wxPython和matplotlib,以及其他一些未在[支持的软件包](http://www.pyinstaller.org/wiki/SupportedPackages)列表中提及的其他软件,如[xlrd](http://pypi.python.org /的PyPI/xlrd)。过去几周,我一直在使用PyInstaller和所有这三个软件包来完成一个项目,而且它几乎是无痛的。 – ChrisC

+0

@ChrisC我也使用了[cx_freeze](http://cx-freeze.sourceforge.net/),因为PyInstaller 1.4不支持Python 2.6。我有点惊讶,py2exe继续使用了很多,因为它自2008年以来一直没有更新。 – Velociraptors

+1

说实话,我没有看到很长时间py2exe和PyInstaller之间的差异,只是默认py2exe ...直到我发现PyInstaller处理了MSVCR * .DLL分发的愚蠢,并且可以打包matplotlib。从那以后一直没有回头。 – ChrisC

1

对于简单的测试,您可以简单地将'site-packages \ matplotlib'中的'mpl-data'文件夹复制到您的应用程序文件夹中。据我所知,'mpl-data'不能被绑定到单个可执行文件中,因此它必须作为一个文件夹包含在你的二进制发行版中。

我使用py2exe通过GUI2Exe,可以冻结我的应用程序,使用matplotlib + numpy/scipy + wx(显然wxagg后端)。我不需要包含_tkagg(它明确排除在GUI2Exe默认设置中对我有用)。

0

Py2exe文档说明了问题的根源并给出了解决方案。它为我工作。 (matplotlib 1.1.0版本,Python 2.7版)

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

因为我没有权限发表评论或评估其他的答案,我有我自己写一个。柯克的回答对我来说是最有价值的帮助。 PyInstaller可能是一种解决方法(没有经过测试),但绝对不是解决问题的技术方案!

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 
)