最新消息:

MAC OSX下pip安装lxml发生xmlversion.h not found问题解决办法

Mac os x admin 3336浏览 0评论

最近由于写爬虫需要用到对html的解析,才搜索了一番之后,从网站的列表中看出lxml的速度相当快.而PyQuery是在其基础上封装的一层库,同时由于采用jquery的访问器语法,成为了我使用的首选.于是,就开始了痛苦的安装过程.

pip应该是最简单安装pip插件的方式之一了.在安装了pip之后,在终端里输入

sudo pip install pyquery

pip开始安装,同时找到了需要依赖的包lxmlcssselect,自动下载lxml以及cssselect.这时候悲剧发生了:

Error: #include "xml/xmlversion.h" not found

调用locate命令,我们可以清晰的看到xmlversion.h是存在的:

summertekiMacBook-Pro:~ summer$ locate xmlversion.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/libxml2/libxml/xmlversion.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/include/libxml2/libxml/xmlversion.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/usr/include/libxml2/libxml/xmlversion.h

查看编译的命令,我们可以看到在lxml编译过程中,有大致这样的输出:

cc    xxxxxxxx -I/usr/include

也就是说,编译的时候头文件的库包有很多,唯独就是少了我们xmlversion.h存在的路径.而且,由于mac是apple定制过的unix系统,其/usr目录下并没有include文件夹.找到问题后,就得想办法解决.

不幸的是,由于不大熟悉pip的执行过程,直接修改命令是不大可能了.那就换个方法,google一番,从stackoverflow找到了相应的解答

export C_INCLUDE_PATH=your_path_libxml:$C_INCLUDE_PATH

看起来能解决问题了~ 执行

export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/:$C_INCLUDE_PATH
sudo pip install pyquery

然后,错误继续…what!!

手动下载lxml包,解压,make.在export的情况下,编译成功了.看来C_INCLUDE_PATH是有效的,但是pip还是不行.

继续google,终于看到了在pip中如何设置一些编译变量.正确的语法是在pip之前先设置好变量,再pip install.

 sudo C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/libxml2:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/libxml2/libxml:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include pip install lxml

同样的,如果需要设置libpath,也在pip之前加入C_LIB_PATH再使用pip. That’s it! Enjoy Pyquery.

 

转载请注明:爱开源 » MAC OSX下pip安装lxml发生xmlversion.h not found问题解决办法

您必须 登录 才能发表评论!