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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21004960/
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-07 01:49:01  来源:igfitidea点击:

No module named lxml.html while running python script on Fedora

pythonlinuxlxmlfedora

提问by Satish Jonnala

I'm trying to run a python script on Fedora Server. I'm getting the following error.

我正在尝试在 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

Doing some research, I found it needs python-lxml package to run the script. This machine already have some lxml installations. But, I'm not able to make this work.

做了一些研究,我发现它需要 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

Also, it says all of them are up to date when I tried to install the packages. For some reason, its not picking up the lxml module and throwing this error while running the script. I'm new to both Linux and Python. Please provide me any clue to debug this issue.

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

回答by MattDMo

The lxmlpackage needs to be installed separately from any libxmllibraries you may have installed. Use pip install lxml(optionally prefixed by sudoto install globally) to install it.

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

回答by abarnert

Looking at the libxml2-pythonpackage, it's pretty clearly the stock Python bindings for libxml2, not the third-party lxmlbindings. These are notthe same things, and they're maintained entirely separately.

查看libxml2-python包,很明显是 的 Python 绑定libxml2,而不是第三方lxml绑定。这些不是相同的东西,它们完全分开维护。

As the docssay:

正如文档所说:

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.

该发行版包括一组 Python 绑定,这些绑定保证在未来作为库的一部分进行维护,尽管 Python 接口尚未达到 C API 的完整性。

请注意,一些 Python 纯粹主义者不喜欢默认的 Python 绑定集,而不是抱怨,我建议他们查看 lxml 的更多 Pythonic 绑定 libxml2 和 libxslt 并检查邮件列表。



How can you tell that libxml2-pythonis the former rather than the latter? Well, the links are to http://xmlsoft.orgrather than to http://lxml.de. And if you look at the files, it installs things with names like libxml2.py, not lxml.

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

So, you can't import lxml because you didn't install lxml.

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

According to rpmfind, there are python-lxmlpackages for a variety of distributions. If there's one for yours, just install that, instead of (or in addition to, if you want both) libxml2-python.

根据 rpmfind,有python-lxml适用于各种发行版的软件包。如果有一个适合您的,只需安装它,而不是(或另外,如果您想要两者)libxml2-python

If not, you will probably want to install it with pip. Assuming you already have pipinstalled, just do this:

如果没有,您可能希望使用pip. 假设您已经pip安装,只需执行以下操作:

sudo pip install lxml

回答by Xiangwu

sudo yum install python-lxml 

or

或者

sudo apt-get install python-lxml