Getting installed version

Peter J. Holzer hjp-python at hjp.at
Sat Jul 21 13:30:32 EDT 2018


On 2018-07-20 08:04:33 +0200, Cecil Westerhof wrote:
> I can get the installed version of a package with:
>     pip2 show cryptography | awk '/^Version: / { print $2 }'
> 
> But I was wondering if there is a better way.

Using pip probably is the most reliable way (for packages installed via
pip).

Most packages have version information embedded, and most of them use
the attribute __version__:

>>> import chardet
>>> chardet.__version__
'2.3.0'
>>> import colorama
>>> colorama.__version__
'0.3.2'
>>> import psycopg2
>>> psycopg2.__version__
'2.5.4 (dt dec pq3 ext)'
>>> import numpy
>>> numpy.__version__
'1.8.2'

But there are exceptions:

>>> import xlrd
>>> xlrd.__VERSION__
'0.9.2'

or even:

>>> import django
>>> django.get_version()
'1.7.11'

So you can't rely on that.

        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp at hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20180721/b27aa248/attachment.sig>


More information about the Python-list mailing list