OS X 10.11 安装 lxml 错误解决方法

Python 2016-04-08 02:24 暂无评论

突然想起国人 @足兆叉虫 开源的 pyspider 爬虫很好用,想起来学习一下。但是在安装过程中发生了一些错误导致无法安装。

pyspider 在安装的时候需要 lxml 模块,但是卡在这一步上无法安装,卡在这一步上无法安装。有的说是没有 lxml2 的 C 库,有的是因为编译 lxml 没有 cc 命令编译导致失败需要安装 command line tools 等等。

推荐解决方法:

  1. 安装 command line tools
xcode-select --install

需要安装 Xcode

  1. Xcode 许可协议
sudo xcodebuild -license

如果没有同意许可协议,在接着安装 lxml 模块的时候一样会出错无法安装。必须要同意 Xcode 许可协议后方可继续安装,如果安装成功了那么下面的步骤可以忽略。如果不成功再往下看!

  1. 指定头文件位置
locate xmlversion.h

首次使用 locate 需要安装建立数据库

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

搜索定位到文件后加入环境变量,在编译的时候会从这个路径下搜索头文件

export C_INCLUDE_PATH=/xxx

这是个大坑!!仔细看报错内容:

    building 'lxml.etree' extension
    creating build/temp.macosx-10.11-intel-2.7
    creating build/temp.macosx-10.11-intel-2.7/src
    creating build/temp.macosx-10.11-intel-2.7/src/lxml
    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 -Isrc/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.11-intel-2.7/src/lxml/lxml.etree.o -w -flat_namespace

    You have not agreed to the Xcode license agreements, please run 'xcodebuild -license' (for user-level acceptance) or 'sudo xcodebuild -license' (for system-wide acceptance) from within a Terminal window to review and agree to the Xcode license agreements.
    Compile failed: command 'cc' failed with exit status 69
    creating tmp
    cc -I/usr/include/libxml2 -I/usr/include/libxml2 -c /tmp/xmlXPathInitgcxwTA.c -o tmp/xmlXPathInitgcxwTA.o

    You have not agreed to the Xcode license agreements, please run 'xcodebuild -license' (for user-level acceptance) or 'sudo xcodebuild -license' (for system-wide acceptance) from within a Terminal window to review and agree to the Xcode license agreements.
    *********************************************************************************
    Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
    Perhaps try: xcode-select --install
    *********************************************************************************
    error: command 'cc' failed with exit status 69

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-z5VTG0/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-Z96M48-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-z5VTG0/lxml

一会儿是没同意 Xcode 许可协议,一会儿是因为没安装 command line tools。。一会儿是没找到 xmlCheckVersion 头文件什么的……

暂无评论