2

On Ubuntu 10.10, I am unable to install lxml to python 2.7. Here are the steps I take.

sudo su -
apt-get install python2.7
apt-get install python-lxml

Note when running the install for python-lxml package, the following appeared:

INFO: using unknown version '/usr/bin/python2.7' (debian_defaults not up-to-date?)"

Importing the module in python2.6 (the version that comes standard with Ubuntu) works. However, importing the module under python2.7 does not. So how does one install Python modules to a non-default Python installation?

Serrano
  • 1,457
  • 20
  • 17
paragbaxi
  • 3,965
  • 8
  • 44
  • 58

4 Answers4

5

Try to install libxml2, libxml2-dev, libxslt, libxslt-dev, python-dev. These are header files. Then try to install lxml again.

Andrew_1510
  • 12,258
  • 9
  • 51
  • 52
  • 2
    using 12.04 the programs are:: libxml2 libxml2-dev libxslt1.1 libxslt1-dev python-dev – Alvin Aug 14 '12 at 00:22
  • 1
    Find it sad that this ( and Alvin's comment) is the correct answer and it's at the bottom. – David Dec 01 '12 at 22:42
4

On Ubuntu 10.10 the python packages installed from the repositories get installed to /usr/lib/python2.6/dist-packages so one option is to add this path to your $PYTHONPATH environmental variable so python2.7 will look to the python2.6 directory for the libs.

What I've done on Ubuntu 10.10 is add

export PYTHONPATH="$PYTHONPATH:/usr/lib/python2.6/dist-packages"

to my .bashrc file, and also to my .gnomerc file. This sets the $PYTHONPATH for python instances started from the shell or from the gnome desktop. You should then be able to import the python libs which you have installed from the Ubuntu repositories in python2.7.

.bashrc and .gnomerc are both located in your home directory; you might have to create .gnomerc if it doesn't already exist. And one caution: I had a syntax error in my .gnomerc which stopped the gnome desktop from loading, and I couldn't log in. I had to use a recovery console to fix this syntax error and then I could log in again.

This seems a little hackish to me, so I'm interested in hearing better solutions.

Buttons840
  • 9,239
  • 15
  • 58
  • 85
0

I have one easiest trick Just open synaptic package manager type "python-lxml" in search box it will show you all the dependencies and available packages select packages which you want to install and hit apply.

user3218971
  • 547
  • 1
  • 6
  • 21
0

Another solution might be to use the following code:

try:
  from lxml import etree
except ImportError:
  try:
    # Python 2.5
    import xml.etree.cElementTree as etree
  except ImportError:
    try:
      # Python 2.5
      import xml.etree.ElementTree as etree
    except ImportError:
      try:
        # normal cElementTree install
        import cElementTree as etree
      except ImportError:
        try:
          # normal ElementTree install
          import elementtree.ElementTree as etree
        except ImportError:
          print("Failed to import ElementTree from any known place")

[Source]

This will import lxml if it is available, or the original ElementTree otherwise.

I use this code for my application on Google App Engine (using Python 2.7): on the server it will use lxml, on my machine it will use ElementTree.

JW.
  • 2,081
  • 1
  • 20
  • 24
  • So, the accepted answer to "how do I install X" is "here's some code to import X". Are installing and importing the same thing? – Buttons840 Feb 15 '12 at 21:32
  • @Buttons840 Well, the question is more "how do I get lxml working on Ubuntu, for use in Python 2.7", for which I show a way to use lxml if available, or (c)ElementTree if it isn't. Since lxml and ElementTree have compatible APIs, this code allows you to support environments where only one is installed. You are right though that it sidesteps the issue, and instead of showing how to install lxml for Python 2.7, it just uses ElementTree instead. – JW. Feb 25 '12 at 13:24