简体   繁体   English

在python中安装lxml模块

[英]Installing lxml module in python

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

Just do:只需这样做:

sudo apt-get install python-lxml

For Python 2 (eg, required by Inkscape):对于 Python 2(例如,Inkscape 需要):

sudo apt-get install python2-lxml

If you are planning to install from source, then albertov's answer will help.如果您打算从源代码安装,那么albertov 的回答会有所帮助。 But unless there is a reason, don't, just install it from the repository.但是除非有原因,否则不要从存储库中安装它。

我解决了它升级 lxml 版本:

pip install --upgrade lxml

You need to install Python's header files (python-dev package in debian/ubuntu) to compile lxml.编译lxml需要安装Python的头文件(debian/ubuntu中的python-dev包)。 As well as libxml2, libxslt, libxml2-dev, and libxslt-dev:除了 libxml2、libxslt、libxml2-dev 和 libxslt-dev:

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

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

pip3 install lxml

For RHEL/CentOS, run "python --version" command to find out Python version.对于 RHEL/CentOS,运行“python --version”命令来查找 Python 版本。 Eg below:例如下面:

$ 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

对于python 3,这对我有用

sudo apt-get install python3-lxml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM