Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unable to find lxml module

Tags:

python

lxml

I wrote a script some times ago that contain

from lxml import etree

But, unfortunatly it is not working anymore. In doubt i checked installation with :

sudo apt-get install python-lxml
sudo pip install lxml
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev

I checked if it could be my python version with :

me@pc:~$ python
Python 2.7.3 (default, Sep 14 2012, 14:11:57) 
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named lxml

My os is ubuntu 12.04.1 LTS with Python 2.7.3.

All seems fine. I can't see what could be the problem.

like image 891
qdelettre Avatar asked Jan 09 '13 16:01

qdelettre


2 Answers

Your solution cited in edit, which use the xml.etree instead of lxml.etree is not the better way to do it, as these module have known incompatibilities, and mainly because lxml is certainly more optimised.

A good way to make a clean environment available is to use virtualenv :

$ virtualenv myproject
$ cd myproject
$ ./bin/pip install lxml # Repeat this with other dependencies
[wait for download and compiling]

Then, use ./bin/python to execute your script. The advantages of this method are :

  • you can have different versions of dependencies between your system and your project
  • even if you break everything in your virtual environment, you will not compromised the rest of your system
  • you do not need root privileges to make an installation

As a reference, a more powerful but slightly more complex way to achieve this is to use buildout, but it can looks like hunting flies with a bazooka if you just want to execute a simple one-file script.

like image 85
Alexis Huet Avatar answered Sep 23 '22 09:09

Alexis Huet


Solved the problem.

It seems that a software that i installed messed out my python path. The python that i was using when calling python in a terminal was the one installed by the software, and the one called by my script was the one installed on my system.

So, i just removed the python path of the software from the path variable of bash.

like image 30
qdelettre Avatar answered Sep 24 '22 09:09

qdelettre