python - 使用 cx_Freeze 导入 matplotlib.pyplot 和 BeautifulSoup

标签 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 试图查看那里。有没有人知道如何解决这个问题,或者可能已经处理过这个问题?

最佳答案

经过一番深入研究,我终于解决了这个问题。对于那些可能遇到同样问题的人,这就是为我解决的问题:

对于 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 repeat , https://bitbucket.org/anthony_tuininga/cx_freeze/issue/59/sre_constantserror-nothing-to-repeat . 相关结论:导致此问题的 cxFreeze 4.3.2 版本有问题。我只是使用 cxFreeze 4.3.1 来构建我的应用程序,问题就解决了。也可以在本地重建 4.3.2 并解决问题,但我没有尝试这个解决方案。

关于python - 使用 cx_Freeze 导入 matplotlib.pyplot 和 BeautifulSoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22263953/

相关文章:

python - 在 Django 中从 http 请求生成 PDF

python - Python 中的简单 3D 图形

python - 如何纠正 ax.annotate 中的坐标移动

python - 简单示例 BeautifulSoup Python

python - Beautiful Soup WebScraping 错误 - ResultSet 对象没有属性 '%s'

python - 如何从 Python 异步运行外部命令?

python - 如何在通过 python 请求模块发送请求时选择特定的密码

python - 填充文本未对齐

python - 如何使用 Pyplot 在所有子图之上设置一个主标题?

python - BeautifulSoup 不在 html 页面中显示某些标签