在MacOS下Python安装lxml报错xmlversion.h not found 报错的解决方案

最近在看一个自动化测试框架的问题,需要用到Lxml库,下载lxml总是报错。

1,使用pip安装lxml

pip install lxml

2,然后报错了,报错内容是:

In file included from src/lxml/lxml.etree.c:139:

src/lxml/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

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/private/var/folders/rc/0lpvj1710n332qg8p2wgcqyr0000gn/T/pip_build_cindy_mac/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/rc/0lpvj1710n332qg8p2wgcqyr0000gn/T/pip-xwdVzY-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /private/var/folders/rc/0lpvj1710n332qg8p2wgcqyr0000gn/T/pip_build_cindy_mac/lxml
Storing debug log for failure in /Users/cindy_mac/Library/Logs/pip.log

3,然后百度了N久N久,借鉴了一个大神的方案,http://blog.marchtea.com/archives/91

4,找到进入Application->XCode,右键选择:显示内容,按照下面代码的路径一步一步往下跟:复制路径:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include

然后在终端输入:

C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2:/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

5,然后再次使用pip安装

pip install lxml

6,看到成功了!!!我眼泪留下来。。。

Downloading/unpacking lxml
  Downloading lxml-3.4.1.tar.gz (3.5MB): 3.5MB downloaded
  Running setup.py (path:/private/var/folders/rc/0lpvj1710n332qg8p2wgcqyr0000gn/T/pip_build_cindy_mac/lxml/setup.py) egg_info for package lxml
    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
      warnings.warn(msg)
    Building lxml version 3.4.1.
    Building without Cython.
    Using build configuration of libxslt 1.1.28
    
    warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
  Running setup.py install for lxml
    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
      warnings.warn(msg)
    Building lxml version 3.4.1.
    Building without Cython.
    Using build configuration of libxslt 1.1.28
    building 'lxml.etree' extension
    cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe -I/usr/include/libxml2 -I/private/var/folders/rc/0lpvj1710n332qg8p2wgcqyr0000gn/T/pip_build_cindy_mac/lxml/src/lxml/includes -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/lxml/lxml.etree.c -o build/temp.macosx-10.9-intel-2.7/src/lxml/lxml.etree.o -w -flat_namespace
    cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.9-intel-2.7/src/lxml/lxml.etree.o -lxslt -lexslt -lxml2 -lz -lm -o build/lib.macosx-10.9-intel-2.7/lxml/etree.so
    building 'lxml.objectify' extension
    cc -fno-strict-aliasing -fno-common -dynamic -arch x86_64 -arch i386 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch x86_64 -arch i386 -pipe -I/usr/include/libxml2 -I/private/var/folders/rc/0lpvj1710n332qg8p2wgcqyr0000gn/T/pip_build_cindy_mac/lxml/src/lxml/includes -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/lxml/lxml.objectify.c -o build/temp.macosx-10.9-intel-2.7/src/lxml/lxml.objectify.o -w -flat_namespace
    cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.9-intel-2.7/src/lxml/lxml.objectify.o -lxslt -lexslt -lxml2 -lz -lm -o build/lib.macosx-10.9-intel-2.7/lxml/objectify.so
    
Successfully installed lxml
Cleaning up...

7,为了保险,然后测试一下:

Cindy-macdeMacBook-Pro:Users cindy_mac$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> exit()

没有报错,我就很开心