使用cxFreeze导入matplotlib.pyplot和BeautifulSoup

5
我正在尝试使用cxFreeze为我的Python脚本编译可执行文件。在我需要导入许多库的情况下,有两个看起来无法与cxFreeze配合使用。特别地,考虑以下test.py脚本:
print('matplotlib.pyplot')
import matplotlib.pyplot

使用cxFreeze编译并运行,将会得到以下输出:

matplotlib cxFreeze问题

另外,以下是测试用的test.py脚本:

print('BeautifulSoup from bs4')
from bs4 import BeautifulSoup

使用cxFreeze编译后,会产生以下输出:

BeautifulSoup cxFreeze problem

我的cxFreeze设置文件如下:

import sys
from cx_Freeze import setup, Executable

setup(
    name = "myname",
    version = "1.0",
    description = "some description",
    executables = [Executable("test.py", base = None)]    
)

我正在运行 Python 3.3 x86,并在 Windows 7 上使用最新的 32 位版本 cxFreeze。

我有一个问题需要解决。首先,我的计算机上不存在 "C:\Python\32-bit..." 目录,所以我不明白为什么 cxFreeze 要在那里查找。是否有人知道如何解决这个问题,或者已经处理过这个问题?

1个回答

8

经过一番挖掘,我终于解决了这个问题。对于那些可能遇到同样问题的人,以下是我解决它的方法:

对于matplotlib的问题:我只需要明确指定cxFreeze包含matplotlib.backends.backend_tkagg即可。 我的设置文件最终看起来像这样:

import sys
from cx_Freeze import setup, Executable
packages = ['matplotlib.backends.backend_tkagg']

setup(
    name = "myname",
    version = "1.0",
    description = "some description",
    options = {'build_exe': {'packages':packages}},
    executables = [Executable("test.py", base = None)]    
)

关于BeautifulSoup问题:网络上有几篇文章处理了这个问题:cx_freeze sre_constants.error nothing to repeathttps://bitbucket.org/anthony_tuininga/cx_freeze/issue/59/sre_constantserror-nothing-to-repeat。相关结论:cxFreeze 4.3.2版本存在问题导致该问题发生。我只需使用cxFreeze 4.3.1版本来构建我的应用程序,问题便解决了。也可能重新本地构建4.3.2版本,问题就会得到解决,但我没有尝试这种解决方案。

1
如果您想使用默认工具栏等功能,则还需要明确包含matplotlib的数据文件。 基本上,您需要在build_exe中的'packages'之外还有 key/value "include_files":[(matplotlib.get_data_path(), "mpl-data")。 不要过多地推销我的答案,但如果有帮助,可以参考:http://stackoverflow.com/questions/22021297/share-image-from-mpldatacursor-with-others/22024663#22024663 - Joe Kington

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接