2012-03-21 56 views
1

我试图使用py2exe使可执行文件。我的.py使用Tkinter的,matplotlib和别的东西:py2exe和matplotlib:与后端错误

import Tkinter 
import math 
from matplotlib.figure import Figure 
from pylab import * 
import random 

的Setup.py我使用,使我的可执行如下:

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" : [ 
           "matplotlib.figure","pylab", "numpy"], 
       'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo', '_cocoaagg', "matplotlib.numerix.fft","sip", "PyQt4._qt", 
          "matplotlib.backends", "matplotlib.backends.backend_qt4agg", 
           "matplotlib.numerix.linear_algebra", "matplotlib.numerix.random_array", 
           "matplotlib.backends.backend_tkagg" 
          '_fltkagg', '_gtk','_tkagg','_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:\Python26\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:\Python26\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']), 
        (r'mpl-data\images',glob.glob(r'C:\Python26\Lib\site-packages\matplotlib\mpl-data\images\*.*')), 
        (r'mpl-data\fonts',glob.glob(r'C:\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\*.*'))] 

# for console program use 'console = [{"script" : "scriptname.py"}] 
setup(windows=[{"script" : "test.py"}], options=opts, data_files=data_files) 

现在,我把我的可执行文件,但是当我运行它,我得到的错误名单:

Traceback (most recent call last): 
File "test.py", line 17, in <module> 
File "pylab.pyc", line 1, in <module> 
File "matplotlib\pylab.pyc", line 259, in <module> 
File "matplotlib\pyplot.pyc", line 94, in <module> 
ImportError: No module named backends 

我不知道很多关于后端,我也试图不从Setup.py文件中排除任何东西,但我得到了同样的错误:“无模块命名后端“

+0

SOLUTION:我解决它通过添加:' “包括”:[ “matplotlib.backends.backend_tkagg”]'到安装文件。 – maupertius 2012-03-21 14:39:12

回答

0

我由用于创建可执行setup.py文件稍微修改解决:

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" : ["matplotlib.backends.backend_tkagg"], 

       'excludes': ['_gtkagg', '_tkagg', '_agg2', '_cairo', '_cocoaagg', "matplotlib.numerix.fft","sip", "PyQt4._qt", 
           "matplotlib.backends.backend_qt4agg", 
           "matplotlib.numerix.linear_algebra", "matplotlib.numerix.random_array", 

          '_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:\Python26\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:\Python26\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']), 
        (r'mpl-data\images',glob.glob(r'C:\Python26\Lib\site-packages\matplotlib\mpl-data\images\*.*')), 
        (r'mpl-data\fonts',glob.glob(r'C:\Python26\Lib\site-packages\matplotlib\mpl-data\fonts\*.*'))] 

# for console program use 'console = [{"script" : "scriptname.py"}] 
setup(windows=[{"script" : "test.py"}], options=opts, data_files=data_files)