python 安装第三方库问题解决办法

python利用pip命令安装第三方库时,常常会出现缺失头文件的情况。例如在安装lxml时,缺失libxml2的某些头文件:

1
2
3
4
5
6
7
8
9
10
/var/folders/xx/3qvwkwxn4xd203232gg6lsnw0000gn/T/xmlXPathInit9kpgjf5d.c:1:10: fatal error: 'libxml/xpath.h' file not found
#include "libxml/xpath.h"
^
1 error generated.
error: command '/usr/bin/clang' failed with exit status 1
*********************************************************************************
Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
Perhaps try: xcode-select --install
*********************************************************************************

按照提示,安装xcode command tools还是不行。
网上查了一下,应该是没有安装libxml2。

mac上使用brew安装一下,命令是:

1
2
brew install libxml2
brew link libxml2 --force

如果还有问题,说明c编译路径里边没有包括某些头文件。因此输入如下命令,将libxml的头文件包含进去:

1
export C_INCLUDE_PATH=/usr/local/Cellar/libxml2/2.9.1/include/libxml2:$C_INCLUDE_PATH

这句命令的路径在不同的mac上因为不同版本的libxml2而有所不同。因此要在安装libxml2后,查看一下具体的路径。

参考文章:
http://stackoverflow.com/questions/17857858/fail-to-install-lxml-in-macos-10-8-4