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

时间:2015-07-10 19:23:58

标签: python beautifulsoup wxpython lxml 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链接)。我试过这个,但仍然没有骰子。

注释掉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位

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

构建成功且没有错误。

1 个答案:

答案 0 :(得分:5)

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

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

所以你的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并尝试执行时,它应该可以工作。