Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

/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.

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.

like image 268
Satish Jonnala Avatar asked Nov 30 '22 00:11

Satish Jonnala


2 Answers

sudo yum install python-lxml 

or

sudo apt-get install python-lxml
like image 109
Xiangwu Avatar answered Dec 06 '22 09:12

Xiangwu


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

As the docs say:

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.


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

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

According to rpmfind, there are python-lxml packages 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.

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

sudo pip install lxml
like image 43
abarnert Avatar answered Dec 06 '22 07:12

abarnert