102

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,392
  • 2
  • 23
  • 36
user563101
  • 1,115
  • 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
  • 29,586
  • 38
  • 176
  • 304
user225312
  • 118,119
  • 66
  • 167
  • 181
88

I solved it upgrading the lxml version with:

pip install --upgrade lxml
Parag Tyagi
  • 8,401
  • 3
  • 39
  • 45
dbarenas
  • 990
  • 8
  • 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
  • 139,281
  • 83
  • 327
  • 490
albertov
  • 2,290
  • 20
  • 15
40

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

pip3 install lxml

yennycheung
  • 531
  • 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,128
  • 1
  • 18
  • 20
5

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
  • 320
  • 1
  • 6
  • 18
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,237
  • 15
  • 13