bundle_files = 1 在使用 matplotlib 的 py2exe 中失败

5 投票
3 回答
3513 浏览
提问于 2025-04-16 10:33

我正在尝试使用py2exe创建一个独立的应用程序,这个应用程序依赖于matplotlib和numpy。我的应用程序代码是:

import numpy as np
import pylab as plt

plt.figure()
a = np.random.random((16,16))
plt.imshow(a,interpolation='nearest')
plt.show()

py2exe的设置代码(从http://www.py2exe.org/index.cgi/MatPlotLib修改而来)是:

from distutils.core import setup
import py2exe
import sys

sys.argv.append('py2exe')

opts = {
    'py2exe': {"bundle_files" : 3,
               "includes" : [ "matplotlib.backends",  
                            "matplotlib.backends.backend_qt4agg",
                            "pylab", "numpy", 
                            "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']
              }
       }

setup(console=[{"script" : "matplotlib_test.py"}], 
                            zipfile=None,options=opts)

现在,当我把bundle_files设置为3或者不设置时,一切都正常,但生成的exe文件无法在没有相同版本Python等配置的机器上运行。如果我把bundle_files设置为1,它会生成一个比较大的exe文件,里面包含了所有东西,但在本地或分发时都无法运行。在这种情况下,我是在一台装有Python 2.6.6的Windows 7机器上创建的,试图在装有Python 2.6.4的XP机器上运行。

在XP机器上运行时出现的错误看起来很奇怪,因为在Windows 7上没有打包时没有错误。而打包后,Windows 7没有报告错误的详细信息,所以我不能确定错误是否相同。无论如何,这里是XP上的错误信息:

Traceback (most recent call last):
  File "matplotlib_test.py", line 2, in <module>
  File "zipextimporter.pyc", line 82, in load_module
  File "pylab.pyc", line 1, in <module>
  File "zipextimporter.pyc", line 82, in load_module
  File "matplotlib\__init__.pyc", line 709, in <module>
  File "matplotlib\__init__.pyc", line 627, in rc_params
  File "matplotlib\__init__.pyc", line 565, in matplotlib_fname
  File "matplotlib\__init__.pyc", line 240, in wrapper
  File "matplotlib\__init__.pyc", line 439, in _get_configdir
RuntimeError: Failed to create C:\Documents and Settings\mnfienen/.matplotlib; c
onsider setting MPLCONFIGDIR to a writable directory for matplotlib configuratio
n data

如果有人能给我指个方向解决这个问题,我将非常感谢!

编辑 1:

我按照William的建议修复了MPLCONFIGDIR的问题,但现在出现了一个新错误:

:Traceback (most recent call last):
  File "matplotlib\__init__.pyc", line 479, in _get_data_path
RuntimeError: Could not find the matplotlib data files

编辑 2: 我通过使用以下方法修复了数据文件的问题:

 data_files=matplotlib.get_py2exe_datafiles()

这导致了一个新的错误:

Traceback (most recent call last):
  File "matplotlib_test.py", line 5, in <module>
    import matplotlib.pyplot as plt
  File "matplotlib\pyplot.pyc", line 78, in <module>
  File "matplotlib\backends\__init__.pyc", line 25, in pylab_setup
ImportError: No module named backend_wxagg

3 个回答

0

解决这个问题有两种方法。

1. 在你的 matplotlib.rc 文件中使用:

backend : TkAgg

2. 或者,在你的 setup.py 文件中的 "includes" 关键字下添加:

"matplotlib.backends.backend_wxagg"

这两种方法都能在 Python 2.6 和 Windows XP 上生成测试图形。

2

我也遇到过同样的问题。我觉得问题出在matplotlib里的pylab上,py2exe似乎在寻找和获取与pylab相关的所有后端时遇到了麻烦。

我通过把所有嵌入的图表改成使用matplotlib.figure来解决这个问题。下面是一个简单的例子,教你如何用matplotlib.figure来画图:

import matplotlib.figure as fg
import numpy as np
fig = fg.Figure()
ax = fig.add_subplot(111)
lines = ax.plot(range(10), np.random.randn(10), range(10), np.random.randn(10))

你不能直接用fig.show()来显示这个图,但它可以嵌入到图形用户界面(GUI)中。我用的是Tkinter:

canvas = FigureCanvasTkAgg(fig, canvas_master)
canvas.show()
1

嗯,Misha Fienen,我想你可能已经知道了,问题似乎出在无法写入你的用户文件夹。只是随便猜猜,你有没有试过按照建议,把MPLCONFIGDIR改成一个简单点的路径,比如“C:\matlibplotcfg\”?

撰写回答