Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the version of an installed module in Python programmatically?

Tags:

python

For the modules:

required_modules = ['nose', 'coverage', 'webunit', 'MySQLdb', 'pgdb', 'memcache']

and programs:

required_programs = ['psql', 'mysql', 'gpsd', 'sox', 'memcached']

Something like:

# Report on the versions of programs installed
for module in required_modules:
    try:
        print module.__version__
    except:
        exit
like image 481
kamal Avatar asked Jan 17 '26 04:01

kamal


1 Answers

Unfortunately, module.__version__ isn't present in all modules.

A workaround is to use a package manager. When you install a library using easy_install or pip, it keeps a record of the installed version. Then you can do:

import pkg_resources
version = pkg_resources.get_distribution("nose").version
like image 68
moraes Avatar answered Jan 19 '26 19:01

moraes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!