问题描述
我正在尝试在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数据目录位于 / 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
I然后添加
(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")
,不要为includes_files列表中的多余内容而烦恼,我会说它们是正确的。但是,我知道我写的东西有效,还没有测试更优雅的方法。
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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!