python - 需要帮助在 os x 10.7 上安装 lxml

标签 python lxml libxml2 pip homebrew

我一直在努力做到 from lxml import etree(顺便说一句,import lxml 工作正常)错误是:

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-            packages/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml/etree.so
Expected in: flat namespace
in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml/etree.so

我使用 pip 来安装 lxml,并使用 homebrew 以正确的架构重新安装 libxml2(或者我认为如此)...有人知道如何修复/诊断这个问题吗?我在 64 位 python 上

最佳答案

lxml 对它使用的第 3 方库有 pip 挑剔,它通常需要比 Apple 提供的版本更新的版本。建议您阅读并遵循说明 here用于在 Mac OS X 上从源代码构建 lxml,包括构建自己的静态链接库。那应该有效。 (我有 pip 惊讶 Homebrew 软件还没有 lxml 配方。)

更新:根据您评论中的有限信息,很难确定到底发生了什么。我怀疑您没有使用您认为的 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 位的;这与您报告的其他一些消息一致。

关于python - 需要帮助在 os x 10.7 上安装 lxml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961577/

相关文章:

python - 插入中的 sqlalchemy 回滚

python-3.x - lxml xmlsyntaxerror : entity 'ouml' not defined

python - 如何强制将所有 namespace 声明附加到根元素?

gcc - Libxml2:对 xmlTextReaderConstName 的 undefined reference

python - 在表单 django-allauth app 的末尾显示 google recaptcha

python - 如何在 Python 中获取文件的 ctime 和/或 mtime,包括时区?

java - 对象内存管理 Python 与 Java

python - MacOS 10.8.4 安装 lxml 失败

c - 使用libxml库函数在c中删除xml节点

dom - 由 br 连接的 XPath 节点文本