在python中安装lxml模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4598229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:29:06  来源:igfitidea点击:

Installing lxml module in python

pythonlxmlpython-import

提问by user563101

while running a python script, I got this error

在运行 python 脚本时,我收到此错误

  from lxml import etree
ImportError: No module named lxml

now I tried to install lxml

现在我尝试安装 lxml

sudo easy_install lmxl

but it gives me the following error

但它给了我以下错误

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 **

Using build configuration of libxslt

使用 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

回答by albertov

You need to install Python's header files (python-dev package in debian/ubuntu) to compile lxml. As well as libxml2, libxslt, libxml2-dev, and libxslt-dev:

编译lxml需要安装Python的头文件(debian/ubuntu中的python-dev包)。除了 libxml2、libxslt、libxml2-dev 和 libxslt-dev:

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

回答by user225312

Just do:

做就是了:

sudo apt-get install python-lxml

For Python 2 (e.g., required by Inkscape):

对于 Python 2(例如,Inkscape 需要):

sudo apt-get install python2-lxml

If you are planning to install from source, then albertov's answerwill help. But unless there is a reason, don't, just install it from the repository.

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

回答by dbarenas

I solved it upgrading the lxml version with:

我解决了它升级 lxml 版本:

pip install --upgrade lxml

回答by yennycheung

If you're running python3, you'll have to do:

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

pip3 install lxml

pip3 install lxml

回答by Krishna Pandey

For RHEL/CentOS, run "python --version" command to find out Python version. E.g. below:

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

$ python --version 
  Python 2.7.12

Now run "sudo yum search lxml" to find out python*-lxml package.

现在运行“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

Now you can choose package as per your Python version and run command like below:

现在您可以根据您的 Python 版本选择包并运行如下命令:

$ sudo yum install python27-lxml.x86_64

回答by Ryan Augustine

If you are encountering this issue on an Alpine based image try this :

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

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

// pip install -r requirements.txt

// pip install -r requirements.txt