使用cxFreeze导入matplotlib.pyplot和BeautifulSoup

时间:2014-03-08 01:48:41

标签: python matplotlib beautifulsoup cx-freeze

我正在尝试使用cxFreeze为我的python脚本编译可执行文件。在我需要为我的脚本导入的许多库中,有两个似乎与cxFreeze失败了。特别要考虑以下test.py脚本:

print('matplotlib.pyplot')
import matplotlib.pyplot

使用cxFreeze进行编译并运行时会提供以下输出:

matplotlib cxFreeze problem

单独,以下test.py脚本:

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

使用cxFreeze编译后,生成以下输出: BeautifulSoup cxFreeze problem

我的cxFreeze的setup.py文件如下所示:

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 个答案:

答案 0 :(得分: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并解决问题,但我没有尝试这个解决方案。