106

while running a python script, I got this error

  from lxml import etree
ImportError: No module named lxml

now I tried to install 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

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
rypel
  • 4,686
  • 2
  • 25
  • 36
user563101
  • 1,155
  • 2
  • 7
  • 5

7 Answers7

111

Just do:

sudo apt-get install python-lxml

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

sudo apt-get install python2-lxml

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

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user225312
  • 126,773
  • 69
  • 172
  • 181
95

I solved it upgrading the lxml version with:

pip install --upgrade lxml
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
dbarenas
  • 1,060
  • 9
  • 8
50

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:

apt-get install python-dev libxml2 libxml2-dev libxslt-dev
David Wolever
  • 148,955
  • 89
  • 346
  • 502
albertov
  • 2,314
  • 20
  • 15
41

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

pip3 install lxml

yennycheung
  • 551
  • 4
  • 6
  • This is what worked for me! I was using the python3 and installing libraries for a previous version of python via `pip`. For `python3`, we should use `pip3`. – Victor Mar 08 '20 at 04:36
7

For python 3 this worked for me

sudo apt-get install python3-lxml
Macumbaomuerte
  • 2,197
  • 2
  • 19
  • 22
6

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

$ python --version 
  Python 2.7.12

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

$ 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:

$ sudo yum install python27-lxml.x86_64
Krishna Pandey
  • 360
  • 1
  • 6
  • 19
0

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

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

// pip install -r requirements.txt

Ryan Augustine
  • 1,455
  • 17
  • 14