在Fed上运行python脚本时没有名为lxml.html的模块

2024-04-25 09:04:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Fedora服务器上运行一个python脚本。我得到以下错误。

/usr/bin/python report_generation.py
Traceback (most recent call last):
File "report_generation.py", line 9, in ?
import lxml.html
ImportError: No module named lxml.html

做了一些研究,我发现它需要python lxml包来运行脚本。这台机器已经安装了一些lxml。但是,我做不到。

yum search libxml

libxml2.i386 : Library providing XML and HTML support
libxml2.x86_64 : Library providing XML and HTML support
libxml2-devel.i386 : Libraries, includes, etc. to develop XML and HTML applications
libxml2-devel.x86_64 : Libraries, includes, etc. to develop XML and HTML applications
libxml2-python.x86_64 : Python bindings for the libxml2 library
libxslt.i386 : Library providing the Gnome XSLT engine
libxslt.x86_64 : Library providing the Gnome XSLT engine
libxslt-devel.i386 : Libraries, includes, etc. to embed the Gnome XSLT engine
libxslt-devel.x86_64 : Libraries, includes, etc. to embed the Gnome XSLT engine
libxslt-python.x86_64 : Python bindings for the libxslt library

而且,它还说当我试图安装这些软件包时,它们都是最新的。出于某种原因,它没有在运行脚本时提取lxml模块并抛出此错误。 我对Linux和Python都是新手。请为我提供调试此问题的任何线索。


Tags: andthelibrarieshtmllibraryetcdevelxml
3条回答

需要将lxml包与您安装的任何libxml库分开安装。使用pip install lxml(可选前缀为sudo进行全局安装)来安装它。

sudo yum install python-lxml 

或者

sudo apt-get install python-lxml

看看^{}包,很明显它是libxml2的常用Python绑定,而不是第三方lxml绑定。这些都是而不是相同的东西,它们是完全分开维护的。

正如the docs所说:

The distribution includes a set of Python bindings, which are guaranteed to be maintained as part of the library in the future, though the Python interface have not yet reached the completeness of the C API.

Note that some of the Python purist dislike the default set of Python bindings, rather than complaining I suggest they have a look at lxml the more pythonic bindings for libxml2 and libxslt and check the mailing-list.


你怎么知道libxml2-python是前者而不是后者?嗯,链接是指向http://xmlsoft.org,而不是指向http://lxml.de。如果你看一下files,它会安装名称类似libxml2.py,而不是lxml的东西。

所以,您不能导入lxml,因为您没有安装lxml。

根据rpmfind,有用于各种发行版的^{}包。如果有一个适合你的,只要安装它,而不是(或者另外,如果你两者都想要的话)安装libxml2-python

如果不是,您可能需要用^{}安装它。假设您已经安装了pip,请执行以下操作:

sudo pip install lxml

相关问题 更多 >