lxml 对它使用的 3rd-party 库有点挑剔,而且它通常需要比 Apple 提供的版本更新的版本。建议您阅读并遵循说明here 在 Mac OS X 上从源代码构建 lxml,包括构建自己的静态链接库。那应该行得通。 (我有点惊讶 homebrew 还没有 lxml 配方。)
更新:根据您的 cmets 中有限的信息,很难确定到底发生了什么。我怀疑你没有使用你认为的 Python 版本。有多种方法可以成功安装 lxml;这是问题的一部分:有太多的选择。与其尝试调试您的设置,这可能是使用 Apple 提供的系统 Python 2.7 在 10.7 上获得工作 lxml 的最简单方法。
$ sudo STATIC_DEPS=true /usr/bin/easy_install-2.7 lxml
你应该可以这样使用lxml.etree:
$ /usr/bin/python2.7
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>> etree.__file__
'/Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so'
>>>
我注意到lxml 静态构建过程不会产生有效的通用构建。在lxml 安装过程中,您可能会看到这样的消息:
ld: warning: ignoring file /private/tmp/easy_install-83mJsV/lxml-2.3.1/build/tmp/libxml2/lib/libxslt.a, file was built for archive which is not the architecture being linked (i386)
假设您机器上的默认架构是 64 位,如果您尝试在 32 位模式下运行:
$ arch -i386 /usr/bin/python2.7
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:06)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
Referenced from: /Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so
Expected in: flat namespace
in /Library/Python/2.7/site-packages/lxml-2.3.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so
>>> ^D
还有你最初报告的错误信息!因此,其根本原因似乎是lxml 构建的静态库(libxml2 等)不是通用的。只要您不需要在 32 位进程中使用 lxml(不太可能用于大多数用途),这应该不是问题。您最初使用的 Python 很可能是 32 位的。这与您报告的其他一些消息一致。