需要什么才能让 BeautifulSoup4+lxml 与 cx_freeze 一起工作?

What's needed to get BeautifulSoup4+lxml to work with cx_freeze?

总结:

我有一个 wxPython/bs4 应用程序,我正在使用 cx_freeze.

构建到一个 exe 中

构建成功且没有错误,但尝试 运行 EXE 导致 BeautifulSoup4 出现 FeatureNotFound 错误。它抱怨我没有安装我的 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 link)。我试过了,还是不行。

注释掉行 soup = BeautifulSoup("<tag>value</tag>", 'xml') 不会导致错误。


版本和文件:

版本:

test.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 时添加这些包:lxmlgzip.

因此您的 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 并尝试可执行文件时,它应该可以工作。