无法在Mac OS X 10.9上安装Lxml

问题:无法在Mac OS X 10.9上安装Lxml

我想安装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,因此不再提供该选项。

有人知道我需要做什么吗?

I want to install Lxml so I can then install Scrapy.

When I updated my Mac today it wouldn’t let me reinstall lxml, I get the following error:

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

I have tried using brew to install libxml2 and libxslt, both installed fine but I still cannot install lxml.

Last time I was installing I needed to enable the developer tools on Xcode but since its updated to Xcode 5 it doesnt give me that option anymore.

Does anyone know what I need to do?


回答 0

您应该为xcode安装或升级命令行工具。在终端上尝试一下:

xcode-select --install

You should install or upgrade the commandline tool for xcode. Try this in a terminal:

xcode-select --install

回答 1

我通过安装和链接libxml2以及libxslt通过brew 在优胜美地上解决了这个问题:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force

如果使用此方法解决了问题,但稍后又弹出,则可能需要以上四行之前运行它:

brew unlink libxml2
brew unlink libxslt

如果您在Homebrew上遇到权限错误,尤其是在El Capitan上,则这是一个有用的文档。本质上,无论OS X是什么版本,都请尝试运行:

sudo chown -R $(whoami):admin /usr/local

I solved this issue on Yosemite by both installing and linking libxml2 and libxslt through brew:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force

If you have solved the problem using this method but it pops up again at a later time, you might need to run this before the four lines above:

brew unlink libxml2
brew unlink libxslt

If you are having permission errors with Homebrew, especially on El Capitan, this is a helpful document. In essence, regardless of OS X version, try running:

sudo chown -R $(whoami):admin /usr/local

回答 2

您可以通过在命令行上运行此命令来解决问题:

 STATIC_DEPS=true pip install lxml

它肯定帮助了我。文档说明

You may solve your problem by running this on the commandline:

 STATIC_DEPS=true pip install lxml

It sure helped me. Explanations on docs


回答 3

我尝试了上面的大多数解决方案,但没有一个对我有用。我正在运行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。

I tried most of the solutions above, but none of them worked for me. I’m running Yosemite 10.10, the only solution that worked for me was to type this in the terminal:

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

EDIT: If you are using virtualenv, the sudo in beginning is not needed.


回答 4

这也困扰了我一段时间。我对python distutils等的内部知识不了解,但是这里的include路径是错误的。我进行了以下丑陋的修改,直到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

This has been bothering me as well for a while. I don’t know the internals enough about python distutils etc, but the include path here is wrong. I made the following ugly hack to hold me over until the python lxml people can do the proper fix.

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

回答 5

全局安装… OS X 10.9.2

xcode-select --install
sudo easy_install pip
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

Installing globally… OS X 10.9.2

xcode-select --install
sudo easy_install pip
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

回答 6

http://lxml.de/installation.html上的安装说明说明:

为了加快测试环境(例如,在持续集成服务器上)的构建速度,请通过设置CFLAGS环境变量来禁用C编译器优化:

CFLAGS="-O0" pip install lxml

instalation instructions on http://lxml.de/installation.html explain:

To speed up the build in test environments, e.g. on a continuous integration server, disable the C compiler optimisations by setting the CFLAGS environment variable:

CFLAGS="-O0" pip install lxml

回答 7

上面的这些在10.9.2上都不适合我,因为编译因以下错误而失败:

clang: error: unknown argument: '-mno-fused-madd' 

这实际上导致最干净的解决方案(请参见[1]中的更多详细信息):

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

pip install lxml

或跟随如果全球安装

sudo pip install lxml

[1] lang错误:未知参数:’-mno-fused-madd’(python软件包安装失败)

None of the above worked for me on 10.9.2, as compilation bails out with following error:

clang: error: unknown argument: '-mno-fused-madd' 

Which actually lead to cleanest solution (see more details in [1]):

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

pip install lxml

or following if installing globally

sudo pip install lxml

[1] clang error: unknown argument: ‘-mno-fused-madd’ (python package installation failure)


回答 8

xcode-select --install
sudo easy_install pip
sudo pip install lxml
xcode-select --install
sudo easy_install pip
sudo pip install lxml

回答 9

Yosemite通过运行以下命令解决了这个问题:

xcode-select install #this may take several minutes.
pip install lxml

I solved this issue on Yosemite by running the following commands:

xcode-select install #this may take several minutes.
pip install lxml

回答 10

对于自制软件,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

With homebrew, libxml2 is hidden to not interfere with the system libxml2, so pip must be helped a little in order to find it.

With bash:

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

With fish:

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

回答 11

OSX 10.9.2

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

OSX 10.9.2

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

回答 12

我尝试了此页面上的所有答案,但没有一个对我有用。我正在运行OS X版本10.9.2

但这绝对有效….就像一个魅力:

ARCHFLAGS = -Wno-error =未使用的命令行参数-将来的硬错误pip install lxml

I tried all the answers on this page, none of them worked for me. I’m running OS X Version 10.9.2

But this definitely works….like a charm:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install lxml


回答 13

不幸的是xcode-select --install,由于我已经拥有最新版本,因此不适用于我。

这很奇怪,但是我通过打开XCode并接受条款与条件解决了这个问题。重新运行pip install lxml后未返回任何错误。

Unfortunately xcode-select --install did not work for me as I already had the latest version.

It’s very strange but I solved the issue by opening XCode and accepting the Terms & Conditions. Re-running pip install lxml returned no errors after.


回答 14

从pip(lxml 3.6.4)成功安装后,导入lxml.etree模块时出现错误。

我一直在不停地搜索以将其安装为scrapy的必要条件,并尝试了所有选项,但最终这对我有用(mac osx 10.11 python 2.7):

$ STATIC_DEPS=true sudo easy_install-2.7 "lxml==2.3.5"

较旧的lxml版本似乎可以与etree模块一起使用。

Pip通常可以忽略软件包的指定版本,例如,当您在pip缓存中有较新的版本时,即为easy_install。该'-2.7'选项适用于python版本,如果要安装python 3.x,则忽略此选项。

After successful install from pip (lxml 3.6.4) I was getting an error when importing the lxml.etree module.

I was searching endlessly to install this as a requisite for scrapy, and tried all the options, but finally this worked for me (mac osx 10.11 python 2.7):

$ STATIC_DEPS=true sudo easy_install-2.7 "lxml==2.3.5"

The older version of lxml seem to work with etree module.

Pip can often ignore the specified version of a package, for example when you have the newer version in the pip cache, thus the easy_install. The '-2.7' option is for python version, omit this if you are installing for python 3.x.


回答 15

就我而言,在通过以下方式安装lxml之前,必须关闭Kaspersky Antivirus

pip install lxml

In my case, I must shutdown Kaspersky Antivirus before installing lxml by:

pip install lxml

回答 16

我正在使用OSX 10.9.2,但出现相同的错误。

XCode命令行工具的安装不适用于此特定版本的OSX。

我认为解决此问题的更好方法是使用以下命令进行安装:

$ CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 pip install lxml

这类似于jdkoftinoff的修复程序,但不会永久更改系统。

I am using OSX 10.9.2 and I get the same error.

Installation of the XCode command line tools does not help for this particular version of OSX.

I think a better approach to fix this is to install with the following command:

$ CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 pip install lxml

This is similar to jdkoftinoff’ fix, but does not alter your system in a permanent way.


回答 17

我遇到了同样的问题,经过几天的工作,我在OS X 10.9.4上使用Python 3.4.1解决了这个问题。

这是我的解决方案,

根据从lxml.de 安装lxml

lxml的macport可用。尝试类似port install py25-lxml的操作

如果您没有MacPort,请从MacPort.org安装它。这很容易。您可能还需要编译器来安装XCode编译工具,使用xcode-select --install

首先,我通过将该端口更新为最新版本sudo port selfupdate

然后我输入sudo port install libxml2,几分钟后,您应该看到libxml2安装成功。可能您可能还需要libxslt安装lxml。要安装libxslt,请使用:sudo port install libxslt

现在,只需键入pip install lxml,它应该可以正常工作。

I met the same question and after days of working I resolved this problem on my OS X 10.9.4, with Python 3.4.1.

Here’s my solution,

According to installing lxml from lxml.de,

A macport of lxml is available. Try something like port install py25-lxml

If you do not have MacPort, install it from MacPort.org. It’s quite easy. You may also need a compiler, to install XCode compiling tools, use xcode-select --install

Firstly I updated my port to the latest version via sudo port selfupdate,

Then I just type sudo port install libxml2 and several minutes later you should see libxml2 installed successfully. Probably you may also need libxslt to install lxml. To install libxslt, use:sudo port install libxslt.

Now, just type pip install lxml, it should work fine.


回答 18

在编译之前,将xmlversion.h的路径添加到您的环境中。

$ set INCLUDE=$INCLUDE:/private/tmp/pip_build_root/lxml/src/lxml/

但是请确保我提供的路径中包含xmlversion.h文件。然后,

$ python setup.py install

before compiling add the path that to xmlversion.h into your environment.

$ set INCLUDE=$INCLUDE:/private/tmp/pip_build_root/lxml/src/lxml/

But make sure the path I’ve provided has the xmlversion.h file located inside. Then,

$ python setup.py install

回答 19

pip没有为我工作。我去了 https://pypi.python.org/pypi/lxml/2.3 并下载了macosx .egg文件:

https://pypi.python.org/packages/2.7/l/lxml/lxml-2.3-py2.7-macosx-10.6-intel.egg#md5=52322e4698d68800c6b6aedb0dbe5f34

然后使用命令行easy_install安装.egg文件。

pip did not work for me. I went to https://pypi.python.org/pypi/lxml/2.3 and downloaded the macosx .egg file:

https://pypi.python.org/packages/2.7/l/lxml/lxml-2.3-py2.7-macosx-10.6-intel.egg#md5=52322e4698d68800c6b6aedb0dbe5f34

Then used command line easy_install to install the .egg file.


回答 20

这篇文章链接到一个适用于我的解决方案,在Mac OS X 10.9上适用于 Python3,lxml和“未找到符号:_lzma_auto_decoder”

hth


回答 21

在头发多撕裂和咬牙切齿之后,我用pip卸载了xcode并运行:

easy_install lxml

一切都很好。

After much tearing of the hair and gnashing of the teeth, I uninstalled xcode with pip and ran:

easy_install lxml

And all was well.


回答 22

尝试:

% STATIC_DEPS=true pip install lxml

要么:

% STATIC_DEPS=true sudo pip install lxml

有用!

Try:

% STATIC_DEPS=true pip install lxml

Or:

% STATIC_DEPS=true sudo pip install lxml

It works!