【问题标题】:import lxml fails on OSX after (seemingly) successful install(看似)成功安装后,导入 lxml 在 OSX 上失败
【发布时间】:2012-03-24 19:48:47
【问题描述】:

我正在尝试在 OS X 10.6.8 上为 python 安装 lxml

我根据对安装 lxml 的问题的回答在终端中运行了sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install lxmlhttps://stackoverflow.com/a/6545556/216336

这是该命令的输出:

MYCOMPUTER:~ MYUSERNAME$ sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install lxml
Password:
Searching for lxml
Reading http://pypi.python.org/simple/lxml/
Reading http://codespeak.net/lxml
Best match: lxml 2.3.3
Downloading http://lxml.de/files/lxml-2.3.3.tgz
Processing lxml-2.3.3.tgz
Running lxml-2.3.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ytPLAc/lxml-2.3.3/egg-dist-tmp-NgYLdF
Building lxml version 2.3.3.
Building without Cython.
Using build configuration of libxslt 1.1.24
Adding lxml 2.3.3 to easy-install.pth file

Installed /Library/Python/2.6/site-packages/lxml-2.3.3-py2.6-macosx-10.6-universal.egg
Processing dependencies for lxml
Finished processing dependencies for lxml

这对我来说看起来很成功,但是当我尝试在 python 中导入 lxml 时出现导入错误...

Last login: Sat Mar 24 15:26:04 on ttys000
MYCOMPUTER:~ MYUSERNAME$ python2.7
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named lxml

它似乎已安装到 2.6 并且我正在运行 2.7 的事实似乎是一个线索,但我不知道该怎么办。

【问题讨论】:

    标签: python macos installation lxml python-import


    【解决方案1】:

    您已经安装了较新版本的 Python (2.7.2),但您还需要安装并使用 easy_install 的副本(来自 Distributesetuptools)才能使用它。您最终使用了与 Apple 提供的 Python 2.6 关联的默认 Apple 提供的 easy_install 版本。注意easy_install 输出中的/Library/Python/2.6。 (您可以通过在使用/usr/bin/python2.6 时尝试导入lxml 来验证这一点。)在使用Python 2.7 安装easy_install 后,如果您使用标准python.org,则新的easy_install 应该在您的shell 路径上首先出现安装程序默认值。如果没有,请更新您的PATH

    export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
    

    然后easy_install lxml使用它。在 python.org 2.7.2 中,您不需要添加 ARCHFLAGS 环境变量,但它不会受到影响。

    【讨论】:

      【解决方案2】:

      你应该能够运行:

      sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install-2.7 lxml
      

      这将使用 2.7 安装附带的 easy_install 版本。

      【讨论】:

      • 在 OS X 10.6 上,没有预装 Python 2.7。 OP 使用来自 OS X 的 python.org 安装程序中的 Python 2.7.2,不包括 easy_install。您必须手动安装 Distribute 或 setuptools。但是,一旦您安装了它,将其调用为 easy_install-2.7 是个好主意。
      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2014-04-18
      • 2015-06-08
      • 1970-01-01
      • 2015-02-11
      • 2014-12-01
      • 2015-05-23
      • 1970-01-01
      相关资源
      最近更新 更多