在python中安装lxml模块

当运行一个python脚本时,我得到了这个错误

from lxml import etree ImportError: No module named lxml 

现在我试图安装lxml

 sudo easy_install lmxl 

但它给了我以下错误

 Building lxml version 2.3.beta1. NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available. ERROR: /bin/sh: xslt-config: not found ** make sure the development packages of libxml2 and libxslt are installed ** 

使用libxslt的构buildconfiguration

 src/lxml/lxml.etree.c:4: fatal error: Python.h: No such file or directory compilation terminated. error: Setup script exited with error: command 'gcc' failed with exit status 1 

做就是了:

 sudo apt-get install python-lxml 

如果你打算从源代码安装,那么albertov的答案将有所帮助。 但是,除非有理由,否则,只需从存储库安装它。

你需要安装Python的头文件(debian / ubuntu中的python-dev包)来编译lxml。 以及libxml2,libxslt,libxml2-dev和libxslt-dev:

 apt-get install python-dev libxml2 libxml2-dev libxslt-dev 

我解决了升级lxml版本:

 pip install --upgrade lxml 

如果你正在运行python3,你必须这样做:

pip3 install lxml

对于RHEL / CentOS,运行“python –version”命令查找Python版本。 例如下面:

 $ python --version Python 2.7.12 

现在运行“sudo yum search lxml”找出python * -lxml包。

 $ sudo yum search lxml Failed to set locale, defaulting to C Loaded plugins: priorities, update-motd, upgrade-helper 1014 packages excluded due to repository priority protections ============================================================================================================= N/S matched: lxml ============================================================================================================= python26-lxml-docs.noarch : Documentation for python-lxml python27-lxml-docs.noarch : Documentation for python-lxml python26-lxml.x86_64 : ElementTree-like Python bindings for libxml2 and libxslt python27-lxml.x86_64 : ElementTree-like Python bindings for libxml2 and libxslt 

现在你可以根据你的Python版本来select软件包,像下面这样运行命令:

 $ sudo yum install python27-lxml.x86_64