我想安装Lxml,这样我就可以安装Scrapy了。

当我更新我的Mac今天,它不让我重新安装lxml,我得到以下错误:

In file included from src/lxml/lxml.etree.c:314:
/private/tmp/pip_build_root/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
#include "libxml/xmlversion.h"
         ^
1 error generated.
error: command 'cc' failed with exit status 1

我尝试使用brew安装libxml2和libxslt,两者都安装好了,但我仍然无法安装lxml。

上次安装时,我需要在Xcode上启用开发人员工具,但由于它更新到Xcode 5,它不再给我这个选项了。

有人知道我要做什么吗?


当前回答

这也困扰了我一段时间。我不太了解python distutils等的内部原理,但这里的include路径是错误的。我做了以下丑陋的hack,直到python lxml的人能做适当的修复。

sudo ln -s  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2/libxml/ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml

其他回答

对于homebrew, libxml2是隐藏的,以不干扰系统libxml2,因此必须帮助pip才能找到它。

bash:

LDFLAGS=-L`brew --prefix libxml2`/lib CPPFLAGS=-I`brew --prefix libxml2`/include/libxml2 pip install --user lxml

与鱼:

env LDFLAGS=-L(brew --prefix libxml2)/lib CPPFLAGS=-I(brew --prefix libxml2)/include/libxml2 pip install --user lxml

OSX 10.9.2

sudo env ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future STATIC_DEPS=true pip install lxml

Try:

% STATIC_DEPS=true pip install lxml

Or:

% STATIC_DEPS=true sudo pip install lxml

它的工作原理!

我尝试了上面的大多数解决方案,但没有一个对我有效。我正在运行Yosemite 10.10,唯一适合我的解决方案是在终端中输入这个:

sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

编辑:如果你正在使用virtualenv,开头的sudo是不需要的。

这篇文章链接了一个对我有用的解决方案 在Mac OS X 10.9上,Python3, lxml和“Symbol not found: _lzma_auto_decoder”

hth