9

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.

qdelettre
  • 1,873
  • 5
  • 25
  • 36
  • 1
    This is what `virtualenv` was made for. – Fredrick Brennan Jan 09 '13 at 16:32
  • What was the result of `pip install lxml` - it looks odd that the required libraries to compile it, are coming after it... – Jon Clements Jan 09 '13 at 16:40
  • What was the output of `sudo pip install lxml`? Additionally, if you want to use `pip` to install `lxml`, you'll need to install the `python-dev` package. – Thomas Orozco Jan 09 '13 at 16:40
  • I don't think creating a virtualenv for each individual script is efficient. sudo pip install lxml output is : Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/dist-packages Cleaning up... – qdelettre Jan 10 '13 at 07:50
  • 4
    When you import `etree` from `xml` you are *not* getting the same module as the `etree` module provided by `lxml`. If you can't import `lxml` then that module is not installed where your Python can find it. – larsks Jan 15 '13 at 13:18
  • try `/usr/bin/python -c 'import lxml'`. If it fails, reinstall lxml: `sudo apt-get install python-lxml --reinstall` – jfs Jan 15 '13 at 13:22

6 Answers6

8

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.

Alexis Huet
  • 755
  • 4
  • 11
  • I know that virtual env and pip are usefull, BUT as i said creating a virtual env for each of my script is not the purpose. See my answer to understand what was my problem ! – qdelettre Jan 19 '13 at 15:36
4

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.

qdelettre
  • 1,873
  • 5
  • 25
  • 36
  • yes, this seems to be the problem for me also. In my case it conflicts with MAMP. The `sudo easy_install lxml` shows: `Building against libxml2/libxslt in the following directory: /Applications/MAMP/Library/lib .......... /tmp/easy_install-KzJaeI/lxml-3.2.5/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found`. The command `echo $PATH` displays `$ echo $PATH /Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php5.5.26/bin:/Users/admin/.composer/vendor/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin` – user9869932 Aug 31 '16 at 15:42
0

I had this problem when running tests in PyCharm. I could import lxml when using the terminal and virtual environment, but using the same virtual environment to run the tests resulted in the ImportError.

In my case upgrading from lxml 3.3.5 to 4.2.5 fixed the problem.

James Bradbury
  • 1,708
  • 1
  • 19
  • 31
0

if youre dealing with conda, i made the "discovery, that my conda install lxml does not work propperly after "successfully" installing

try uninstalling and reinstalling with pip, that worked for me

conda remove lxml

pip install lxml
John Lloyd
  • 157
  • 1
  • 11
-1

This likely has something do to with your Python environment. Here are three troubleshooting methods, from least to most cumbersome

1) Re-install via pip

pip uninstall lxml
pip install lxml

2) Re-Install via IDE

  • uninstalling it from the terminal (pip uninstall lxml)
  • adding from lxml import etree at the beginning of a python file
  • letting my IDE (PyCharm) show a missing import error
  • hovering over the error and installing it from the IDE via the error-fixing pop-up (not terminal)

3) Re-create virtualenv

Assuming you're working in a virtual environment (if not, shame to you and your family): delete and create a new environment.

Tyler Dane
  • 951
  • 1
  • 14
  • 25
-2

For Python 3.6, it is very tricky.

I have the same problem and try to install the latest version, lxml 3.8. But I still get the error. And then I use lxml 3.7.3, then it is ok.