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

dth*_*hor 1 python lxml wxpython beautifulsoup cx-freeze

摘要:

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

构建成功没有错误,但尝试运行EXE会导致FeatureNotFoundBeautifulSoup4出错.它抱怨我没有安装我的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?
Run Code Online (Sandbox Code Playgroud)

我已经尝试过的:

我发现有些人说我需要在构建脚本中包含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()
Run Code Online (Sandbox Code Playgroud)

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,
)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

构建成功,没有错误.

dth*_*hor 5

经过大量调查,进入bs4和他们的建设者,我终于发现我还需要添加gzip.

所以最后,cx_freeze脚本至少需要在使用bs4 + lxml:lxml和构建exe时添加的这些包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,
)
Run Code Online (Sandbox Code Playgroud)

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