【问题标题】:bundle_files = 1 fails with py2exe using matplotlibbundle_files = 1 使用 matplotlib 的 py2exe 失败
【发布时间】:2011-01-24 22:13:31
【问题描述】:

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

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 机器上运行时遇到的错误看起来很奇怪,因为在没有捆绑的情况下,我在 Win 7 上没有错误。通过捆绑,Win 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

【问题讨论】:

    标签: python matplotlib py2exe


    【解决方案1】:

    我遇到了同样的问题。我认为问题是由 matplotlib 中的 pylab 引起的,py2exe 似乎无法找到和获取与 pylab 相关的所有后端。

    我通过将所有嵌入式绘图更改为使用 matplotlib.figure 而不是 pylab 解决了这个问题。这是一个关于如何使用 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 中。我用过 Tkinker:

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

    【讨论】:

      【解决方案2】:

      好吧,Misha Fienen,我猜它似乎无法写入您可能已经知道的用户文件夹。只是在黑暗中刺伤,但是您是否尝试过测试如果您遵循建议并将 MPLCONFIGDIR 更改为更基本的内容(例如“C:\matlibplotcfg\”)会发生什么?

      【讨论】:

      • 谢谢威廉。因此,我尝试更改 MPLCONFIGDIR,但这是一个红鲱鱼。事实证明,如果没有将 MPLCONFIGDIR 设置为可写位置,即使是基本测试 .py 代码也无法在 XP 机器上运行,但现在我得到了一个新错误。请参阅上面原始问题中的编辑.....
      • 看起来这是一个比最初看起来更严重的问题。对于您当前的新错误,您可以尝试将“matplotlib.backends.backend_tkagg”添加到您的包含列表中,但有些事情告诉我有一个更简单更彻底的解决方案浮动=/
      • 有趣——我尝试将“matplotlib.backends.backend_tkagg”放在排除项中,但不包括在内。我同意,我想,在某个地方有一条更大的鱼可以炸。
      【解决方案3】:

      解决问题的方法有两种。

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

      后端:TkAgg

      2.- 或者,在 setup.py “includes” 键中添加:

      “matplotlib.backends.backend_wxagg”

      在 Python 2.6、windows XP 两种方式都可以生成测试图

      【讨论】:

        猜你喜欢
        • 2011-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 2011-09-23
        • 2016-03-17
        相关资源
        最近更新 更多