Skip to main content
 首页 » 编程设计

python之让 BeautifulSoup4+lxml 与 cx_freeze 一起工作需要什么

2025年05月04日33zhwl

总结:

我有一个 wxPython/bs4 应用程序,我正在使用 cx_freeze 将其构建到一个 exe 文件中。

构建成功且没有错误,但尝试运行 EXE 结果为 FeatureNotFound来自 BeautifulSoup4 的错误。它提示我没有安装我的 lxml 库。

我已经将程序剥离到它的最小状态,但仍然出现错误。

有没有其他人成功地使用 cx_freeze 构建了 bs4 应用程序?

请查看下面的详细信息,如果您有任何想法,请告诉我。

谢谢,


详情

完整的错误回溯:

我已将应用程序简化为最基本的状态,但仍然出现错误。我在 Python 3.4 上也遇到同样的错误。

Traceback (most recent call last): 
  File "C:\WinPython27\python-2.6.7\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module> 
    exec(code, m.__dict__) 
  File "test.py", line 6, in <module> 
  File "C:\WinPython27\python-2.6.7\lib\site-packages\bs4\__init__.py", line 152, in __init__ 
    % ",".join(feautres)) 
FeatureNotFound: Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library? 

我已经尝试过的:

我发现有些人说我需要在构建脚本中包含 lxml 及其依赖项:http://sourceforge.net/p/cx-freeze/mailman/message/27973651/ (对不起,SF 链接)。我试过了,但仍然没有骰子。

注释掉 soup = BeautifulSoup("<tag>value</tag>", 'xml') 行结果没有错误。


版本和文件:

版本:

  • lxml 3.4.4
  • BeautifulSoup4 4.3.2
  • Python 2.7.6(32 位)和 Python 3.4.3(64 位)
  • cx_Freeze 4.3.4
  • wxPython 3.0-msw
  • Windows 7 Professional SP1,64 位

测试.py

此文件是简化后的应用程序,但仍会出现错误。

# -*- coding: utf-8 -*- 
from __future__ import print_function 
from bs4 import BeautifulSoup 
import wx 
 
soup = BeautifulSoup("<tag>value</tag>", 'xml') 
 
app = wx.App() 
frame = wx.Frame(None, wx.ID_ANY, "test frame") 
frame.Show() 
app.MainLoop() 

build_executables.py

这是我正在使用的(简化的)构建脚本。

# -*- coding: utf-8 -*- 
from cx_Freeze import setup, Executable 
build_exe_opts = {"silent": False, } 
 
base = "Win32GUI" 
 
exes_to_build = [Executable("test.py", base=base), 
                 ] 
 
setup( 
    name="test", 
    version="0.0.1", 
    description="FTI test program editor and diff tool.", 
    options={"build_exe": build_exe_opts}, 
    executables=exes_to_build, 
) 

cx_freeze 日志

我不打算在这里包括整个日志,但我确实运行了 python build_executables.py build >> C:\temp\build_log.txt所以可以查找任何需要的东西。

以下是其中包含“lxml”的行:

  Name                      File 
  ----                      ---- 
P bs4                       C:\WinPython27\python-2.7.6\lib\site-packages\bs4\__init__.py 
P bs4.builder               C:\WinPython27\python-2.7.6\lib\site-packages\bs4\builder\__init__.py 
m bs4.builder._html5lib     C:\WinPython27\python-2.7.6\lib\site-packages\bs4\builder\_html5lib.py 
m bs4.builder._htmlparser   C:\WinPython27\python-2.7.6\lib\site-packages\bs4\builder\_htmlparser.py 
m bs4.builder._lxml         C:\WinPython27\python-2.7.6\lib\site-packages\bs4\builder\_lxml.py 
m bs4.dammit                C:\WinPython27\python-2.7.6\lib\site-packages\bs4\dammit.py 
m bs4.element               C:\WinPython27\python-2.7.6\lib\site-packages\bs4\element.py 
... 
P lxml                      C:\WinPython27\python-2.7.6\lib\site-packages\lxml\__init__.py 
m lxml.etree                C:\WinPython27\python-2.7.6\lib\site-packages\lxml\etree.pyd 
... 
copying C:\WinPython27\python-2.7.6\lib\site-packages\lxml\etree.pyd -> build\exe.win32-2.7\lxml.etree.pyd 

构建成功,没有错误。

请您参考如下方法:

经过大量调查,进入 bs4 及其构建器,我终于发现我还需要添加 gzip。

所以最后,cx_freeze 脚本至少需要在使用 bs4 + lxml 构建 exe 时添加这些包:lxml 和 gzip。

因此您的 build_executables.py 脚本应该如下所示:

# -*- coding: utf-8 -*- 
from cx_Freeze import setup, Executable 
build_exe_opts = {"silent": False, 
                  "packages": ['lxml', 'gzip'], 
                  } 
base = "Win32GUI" 
 
exes_to_build = [Executable("test.py", base=base)] 
 
setup( 
    name="test", 
    version="0.0.1", 
    description="blah blah blah", 
    options={"build_exe": build_exe_opts}, 
    executables=exes_to_build, 
) 

然后当您运行 python build_executables.py build 并尝试可执行文件时,它应该可以工作。