Import error for lxml in python

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.


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.

Tags:

Python

Lxml