在python中安装lxml模块

时间:2011-01-04 20:45:14

标签: python lxml python-import

在运行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的构建配置

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

6 个答案:

答案 0 :(得分:99)

只是做:

sudo apt-get install python-lxml

如果您打算从源代码安装,那么albertov's answer会有所帮助。但除非有原因,否则不要只是从存储库中安装它。

答案 1 :(得分:49)

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

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

答案 2 :(得分:48)

我解决了升级lxml版本的问题:

pip install --upgrade lxml

答案 3 :(得分:20)

如果您正在运行python3,则必须执行以下操作:

pip3 install lxml

答案 4 :(得分:2)

对于python 3,这对我有用

sudo apt-get install python3-lxml

答案 5 :(得分:0)

如果您在基于Alpine的图像上遇到此问题,请尝试以下方法:

apk add --update --no-cache g++ gcc libxml2-dev libxslt-dev python-dev libffi-dev openssl-dev make

// pip install -r requirements.txt