44

How can I find the installed python-lxml version in a Linux system?

>>> import lxml
>>> lxml.__version__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'

>>> from pprint import pprint
>>> pprint(dir(lxml))
['__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '__path__',
 'get_include',
 'os']
>>>

Can't seem to find it

Niklas9
  • 8,816
  • 8
  • 37
  • 60

6 Answers6

50

You can get the version by looking at etree:

>>> from lxml import etree
>>> etree.LXML_VERSION
(3, 0, -198, 0)

Other versions of interest can be: etree.LIBXML_VERSION, etree.LIBXML_COMPILED_VERSION, etree.LIBXSLT_VERSION and etree.LIBXSLT_COMPILED_VERSION.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
12

I assume you want to determine lxml's version programatically from Python. Since lxml does not provide this information via way of a typilca __version__ attribute on the top-level package you will have to resort to using setuptools' pkg_resources.require() function:

>>> from pkg_resources import require
>>> match = require("lxml")
>>> match
[lxml 3.3.0beta1 (/home/prologic/lib/python2.7/site-packages)]
>>> lxml = match[0]
>>> lxml.version
'3.3.0beta1'
James Mills
  • 18,669
  • 3
  • 49
  • 62
10

Here are two more ways to do it, with minimal typing. You could do it with pip from the command line:

$ pip freeze | grep lxml
lxml==3.2.5

Since you installed from ubuntu repository with apt-get you can also use dpkg:

$ dpkg -l python-lxml
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                     Version                  Description
+++-========================-========================-================================================================
ii  python-lxml              2.2.4-1                  pythonic binding for the libxml2 and libxslt libraries
Aryeh Leib Taurog
  • 5,370
  • 1
  • 42
  • 49
9

I'm surprised that nobody suggested

pip show lxml

grigy
  • 6,696
  • 12
  • 49
  • 76
4

You can use pip as well:

import pip
lxml_package = [pckg for pckg in pip.get_installed_distributions() 
                if pckg.project_name == 'lxml'][0] # assuming lxml is installed
print lxml_package.version
Alexander Zhukov
  • 4,357
  • 1
  • 20
  • 31
2
from lxml import etree
etree.__version__
chroming
  • 358
  • 2
  • 7