python - matplotlib 数据的 cx_freeze 错误

标签 python matplotlib debian cx-freeze

我尝试在 Debian 8 计算机上使用 cx_freeze 卡住 Python 程序,但遇到以下错误消息:

copying /usr/lib/python2.7/dist-packages/matplotlib/mpl-data -> build/exe.linux-x86_64-2.7/mpl-data
error: [Errno 2] No such file or directory: '/usr/lib/python2.7/dist-packages/matplotlib/mpl-data'

我的setup.py文件包含:

from cx_Freeze import setup, Executable

buildOptions = {
    "excludes": ["PyQt5"]}
# PyQt5 conflicts with PyQt4

base = None

executables = [
    Executable('test_fitfuns.py', base=base)
]

setup(name='testfitfuns',
      version = '1.0',
      description = 'test fit functions',
      options = dict(build_exe = buildOptions),
      executables = executables)

我发现我的mpl-data目录位于“/usr/share/matplotlib/mpl-data”中,所以我尝试将这一行添加到 >构建选项:

"include_files": [("/usr/share/matplotlib/mpl-data", "mpl-data")],

如果我这样做,我的错误将变为:

error: [Errno 21] Is a directory: 'build/exe.linux-x86_64-2.7/mpl-data'

接下来我应该尝试什么?

这是我第一次尝试使用 cx_freeze,所以如果这是一个微不足道的问题,我深表歉意。

最佳答案

问题是由 cx_freeze 认为 mpl-data 目录所在的位置引起的。具体来说,cx_Freeze.hooks 模块中的函数 load_matplotlib() 在 Linux 上创建了错误的路径(但在 Windows 上则不然)。

def load_matplotlib(finder, module):
    """the matplotlib module requires data to be found in mpl-data in the
       same directory as the frozen executable so oblige it"""
    dir = os.path.join(module.path[0], "mpl-data")
    finder.IncludeFiles(dir, "mpl-data")

在包装过程中会自动调用此函数来设置 mpl-data 目录的路径。它实际上应该使用 matplotlib 包提供的 get_data_path() 函数,因为它返回所需的路径。

了解所有这些后,我将以下内容添加到我的 setup.py 文件中。

import cx_Freeze.hooks
def hack(finder, module):
    return
cx_Freeze.hooks.load_matplotlib = hack

我在之后添加了这个

from cx_Freeze import setup, Executable

然后我添加

 (matplotlib.get_data_path(), "mpl-data")

到 build_options_dict 的 include_files 列表

include_files =[              
                (matplotlib.get_data_path(), "mpl-data")
                #you may nay need to include other files 
                ]
build_options_dict ={"path" : apath, 
                     "packages":packages,
                     "include_files" :include_files,
                     "excludes": excludes,
                     "optimize":2
                     }

然后将 build_options_dict 提供给选项字典中的 cx_freeze 设置函数。

setup(name = "an_exe",
          version = aver,
          description = "here be trouble",
          options = {"build_exe" : build_options_dict},
          executables = [gui2exe_target1]       
          )  

现在有些人可能会说这样写会更优雅

def hack(finder, module):
        finder.IncludeFiles(matplotlib.get_data_path(), "mpl-data")

并且不用担心includes_files列表中的额外内容,我想说他们是对的。但是,我知道我写的内容有效,但尚未测试更优雅的方法。

关于python - matplotlib 数据的 cx_freeze 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39000452/

相关文章:

python - 学习变量的预期 tensorflow 模型大小

python - 如何在 python 中创建 3D 高度图

c++ - 从新的和不同的发行版加载 LD_PRELOAD libstdc++.so.6 和 libc.so.6 时出现浮点异常

python - imshow 中 matplotlib 的颜色值?

Python Tkinter : Embed a matplotlib plot in a widget

linux - 删除安静参数后,Debian Wheezy 不显示启动消息

c - 编译某些函数所需的库

python - 比较 int 值

python - 断言失败时继续 Python 的单元测试

python - 如何在训练期间替换损失函数 tensorflow.keras