使用 cx_Freeze 导入 matplotlib.pyplot 和 BeautifulSoup

Posted

技术标签:

【中文标题】使用 cx_Freeze 导入 matplotlib.pyplot 和 BeautifulSoup【英文标题】:Importing matplotlib.pyplot and BeautifulSoup with cxFreeze 【发布时间】:2014-03-08 01:48:41 【问题描述】:

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

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

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

另外,下面的 test.py 脚本:

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

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

我的 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】:

经过一番挖掘,我能够解决这个问题。对于那些可能遇到相同问题的人,这就是为我解决的问题:

对于 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 并解决问题,但我没有尝试此解决方案。

【讨论】:

+1 如果您想使用默认工具栏等,您还需要显式包含 matplotlib 的数据文件。基本上,您希望拥有键/值 "include_files":[(matplotlib.get_data_path(), "mpl-data") 除了'packages'build_exe。不要过多插入我自己的答案,但如果有帮助,请参阅:***.com/questions/22021297/…

以上是关于使用 cx_Freeze 导入 matplotlib.pyplot 和 BeautifulSoup的主要内容,如果未能解决你的问题,请参考以下文章

使用 cx_Freeze、Python3.4 导入 GDAL

使用 cx_Freeze 导入 matplotlib.pyplot 和 BeautifulSoup

cx_Freeze 没有找到一些 TensorFlow 导入

cx_Freeze- ImportError:无法导入名称设置

cx_Freeze ImportError:无法导入名称

使用 win32wnet 导入时使用 cx_freeze 编译后的 DLL 问题