在mac上安装带有python的lxml

在mac上安装带有python的lxml,python,installation,lxml,importerror,elementtree,Python,Installation,Lxml,Importerror,Elementtree,首先,我安装lxml时没有使用pip(MacOS10.6.8上的Python 2.7.2)。然后,我阅读并使用pip再次安装了它(sudopip-install-lxml)。我仍然有一个问题: 我可以导入lxml(import lxml),但不能从lxml导入etree使用。我收到以下错误消息: Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dlo

首先,我安装lxml时没有使用pip(MacOS10.6.8上的Python 2.7.2)。然后,我阅读并使用pip再次安装了它(
sudopip-install-lxml
)。我仍然有一个问题:

我可以导入lxml(
import lxml
),但不能从lxml导入etree使用
。我收到以下错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so
checking whether we are cross compiling... configure: error: in `/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2-2.7.8':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
Traceback (most recent call last):
  File "setup.py", line 115, in <module>
    STATIC_CFLAGS, STATIC_BINARIES),
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/setupinfo.py", line 50, in ext_modules
    libxslt_version=OPTION_LIBXSLT_VERSION)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 198, in build_libxml2xslt
    call_subprocess(configure_cmd, cwd=libxml2_dir, **call_setup)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 158, in call_subprocess
    raise Exception('Command "%s" returned code %s' % (cmd_desc, returncode))
Exception: Command "./configure --without-python --disable-dependency-tracking --disable-shared --prefix=/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2" returned code 1
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ImportError:dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so,2):未找到符号:\u htmlparserschunk
引用自:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so
应为:平面命名空间
在/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so中
然后,我尝试从以下源代码安装lxml,并收到以下错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so
checking whether we are cross compiling... configure: error: in `/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2-2.7.8':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
Traceback (most recent call last):
  File "setup.py", line 115, in <module>
    STATIC_CFLAGS, STATIC_BINARIES),
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/setupinfo.py", line 50, in ext_modules
    libxslt_version=OPTION_LIBXSLT_VERSION)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 198, in build_libxml2xslt
    call_subprocess(configure_cmd, cwd=libxml2_dir, **call_setup)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 158, in call_subprocess
    raise Exception('Command "%s" returned code %s' % (cmd_desc, returncode))
Exception: Command "./configure --without-python --disable-dependency-tracking --disable-shared --prefix=/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2" returned code 1
正在检查我们是否正在交叉编译。。。配置:错误:在“/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2-2.7.8”中:
配置:错误:无法运行C编译程序。
如果要交叉编译,请使用“---host”。
有关详细信息,请参阅“config.log”
回溯(最近一次呼叫最后一次):
文件“setup.py”,第115行,在
静态\u CFLAGS、静态\u二进制文件),
文件“/Users/my_name/Applications/lxml/lxml-2.2.2/setupinfo.py”,第50行,在ext_模块中
libxslt\u version=选项(libxslt\u version)
文件“/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py”,第198行,在build_libxml2xslt中
call_子进程(configure_cmd,cwd=libxml2_dir,**call_setup)
文件“/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py”,第158行,在call_子流程中
引发异常('命令“%s”返回代码%s'(cmd\u desc,returncode))
异常:命令“/configure--without python--disable dependency tracking--disable shared--prefix=/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2”返回代码1
最后,我按照的第二个建议,使用命令行
sudostatic\u DEPS=true/usr/bin/easy\u install-2.7lxml
。它在苹果提供的系统Python2.7上安装了lxml,而不是我目前使用的版本。积极的一点是:如果我运行applepython,我可以从lxml导入etree

缺点:我仍然不知道如何在另一个版本的python上安装lxml。有什么想法吗


我目前正在使用
/Library/Frameworks/Python.framework/Versions/2.7/bin/Python
您需要为正在使用的Python版本安装一个单独的
easy\u install
。有关更多详细信息,请参见答案。然后可以使用它运行
easy\u install
命令:

STATIC_DEPS=true easy_install-2.7 lxml
更新:根据您的评论,您现在报告了一个权限错误,显示了另一个Python路径,该路径似乎是MacPorts安装的Python:
/opt/local/Library/Frameworks
。您需要弄清楚要使用哪种Python。事实上,如果您想使用MacPorts one,只需安装MacPorts提供的
lxml
端口即可。这是最简单的解决办法

$ sudo port selfupdate
$ sudo port install py27-lxml
否则,您需要使用另一个(python.org?)python2.7.2安装
easy\u install
。我建议使用更现代的“setuptools”,您需要确保使用正确的Python:

$ curl -O http://python-distribute.org/distribute_setup.py
$ export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
$ which python2.7   # should be in the path above
$ python2.7 distribute_setup.py
$ STATIC_DEPS=true easy_install-2.7 lxml

在这个问题上,不同版本的OSX可能存在重复,但可能足以让您获得成功,从源代码构建lxml不起作用。因此,首先,我使用
sh setuptools-0.6c11-py2.7.egg安装了easy_install(下载egg后,我修改了路径(
export path=“/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}”
),最后我安装了lxml(
STATIC\u DEPS=true easy\u install-2.7 lxml
)。最后一个命令仍有问题:
错误:无法在安装目录中创建或删除文件
尝试在安装目录中添加或删除文件时发生以下错误:[Errno 13]权限被拒绝:'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/test-easy-install-1180.write test'}
顺便问一句,与
STATIC\u DEPS=true easy\u install lxml
有什么区别吗?(不带-2.7)请参阅更新的答案。Distribution和setuptools安装这两个版本(
easy\u install
)和版本(
easy\u install-2.7
)命令的副本。在特定的Python实例中,它们应该会产生相同的结果。@Antonin:添加的
-2.7
指的是easy_install的特定版本,因为您安装了多个Python。如果您使用macports,我真的想把它扔出去……我建议完全删除它,然后使用Ho梅布雷。