问题描述
我正在尝试在 Debian 8 机器上使用 cx_freeze 冻结 Python 程序,但遇到以下错误消息:
I'm trying to freeze a Python program with cx_freeze, on a Debian 8 machine, but I run into this error message:
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"
中,所以我尝试将此行添加到 >buildOptions
:
I figured out that my mpl-data directory is in "/usr/share/matplotlib/mpl-data"
, so I tried adding this line to buildOptions
:
"include_files": [("/usr/share/matplotlib/mpl-data", "mpl-data")],
如果我这样做,我的错误会变成:
If I do this, my error becomes:
error: [Errno 21] Is a directory: 'build/exe.linux-x86_64-2.7/mpl-data'
接下来我应该尝试什么?
What should I try next?
这是我第一次尝试使用 cx_freeze,所以如果这是一个微不足道的问题,我深表歉意.
This is my first attempt at using cx_freeze, so I apologize if this is a trivial question.
推荐答案
问题是cx_freeze认为mpl-data目录在哪里引起的.具体来说,cx_Freeze.hooks 模块中的函数 load_matplotlib() 在 Linux 上创建了错误的路径(但在 Windows 上没有).
The problem is caused by where cx_freeze thinks the mpl-data directory lives. Specifically, the function load_matplotlib(), in the cx_Freeze.hooks module, creates the wrong path on Linux (but not on 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() 函数,因为它返回所需的路径.
This function gets called automatically during the wrapping process to set the path to the mpl-data directory. It should really be using the get_data_path() function provided by the matplotlib package as this returns the required path.
了解所有这些后,我将以下内容添加到我的 setup.py 文件中.
Knowing all of this I added the following to my setup.py file.
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 列表
To the include_files list for the build_options_dict
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 设置函数.
The build_options_dict is then supplied to the cx_freeze setup function in the options dictionary.
setup(name = "an_exe",
version = aver,
description = "here be trouble",
options = {"build_exe" : build_options_dict},
executables = [gui2exe_target1]
)
现在有些人可能会说,写出来会更优雅
Now some may say that it would have been more elegant to have just written
def hack(finder, module):
finder.IncludeFiles(matplotlib.get_data_path(), "mpl-data")
不要为包含文件列表中的额外内容而烦恼,我会说他们是对的.但是,我知道我写的东西有效,还没有测试更优雅的方法.
and not to have bothered with the extra stuff in the includes_files list and I would say they are right. However, I know what I wrote works and have yet to test the more elegant method.
这篇关于matplotlib 数据的 cx_freeze 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!