【问题标题】:Error installing python module lxml on osx mavericks在 osx mavericks 上安装 python 模块 lxml 时出错
【发布时间】:2014-01-07 06:37:35
【问题描述】:

这是我的 python 环境:注意 numpy 正在工作

13:41:44/shared:5 $python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from numpy import *
>>> print mat([1,2,3])
[[1, 2, 3]]
>>>

但我无法安装 lxml。我的 python 和/或 os 配置中缺少什么来使这项工作:

13:37:03/shared:4 $pip install --upgrade lxml
Downloading/unpacking lxml
  Downloading lxml-3.2.4.tar.gz (3.3MB): 3.3MB downloaded
  Running 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.2.4.
    Building without Cython.
    Using build configuration of libxslt 1.1.28

    warning: no previously-included files found matching '*.py'
    warning: no files found matching '*.txt' under directory 'src/lxml/tests'
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.2.4.
    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 -mno-fused-madd -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/k4/l5ww32y14svd5wmkgzzftl9m0000gn/T/pip_build_steve/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 -flat_namespace
    clang: warning: argument unused during compilation: '-mno-fused-madd'
    clang: warning: argument unused during compilation: '-flat_namespace'
    In file included from src/lxml/lxml.etree.c:314:
    **/private/var/folders/k4/l5ww32y14svd5wmkgzzftl9m0000gn/T/pip_build_steve/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
    #include "libxml/xmlversion.h"**
             ^
    1 error generated.

【问题讨论】:

    标签: python macos lxml osx-mavericks


    【解决方案1】:

    您似乎缺少 libxml/xmlversion.h

    由于似乎包含 /usr/include/libxml2,我认为最可能的原因是您的系统上没有安装 libxml2。这很可能是由于缺少“命令行工具”。在这里获取它们:https://developer.apple.com/downloads/index.action?=command%20line%20tools

    也可以通过 macports 或 brew 安装 libxml2 来解决(但不要这样做,除非作为最后的手段)。尽可能使用系统库而不是自制软件或 macport 可以让您避免许多不兼容的陷阱。

    【讨论】:

    • 您好,我通过自制软件重新安装了 libxml2。也许升级到 Mavericks 10.9.1 会覆盖之前的安装。会回到这里。
    • 我做了“brew install libxml2”,成功了。但是“pip install --upgrade lxml”仍然失败并出现同样的错误
    • 你检查过 /usr/include/libxml2 存在吗?我敢打赌,在这种情况下,我的建议是安装 Apple 的命令行工具。当系统和自制软件都可用时,首选系统库几乎总能帮你省去一些麻烦。 PS。 homebrew-libxml 没有解决它的原因是 /usr/local/include 在构建时可能不包含在 pip 中。在尝试破解之前,我真的建议检查 /usr/include 并安装命令行工具 - 这可能会解决所有问题:)
    • 你是对的 - /usr/include/libxml2 不存在。我刚刚升级到 10.9.1 - 这是否清除了命令行工具??
    • 是的。 “命令行工具”是特定于操作系统的,每次升级到 OSX 的新“主要版本”时都需要重新安装。