3

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
kamal
  • 9,309
  • 30
  • 100
  • 154
  • 1
    As I understand it, there's no simple, perfect way to do this. But [the answers to this prior question](http://stackoverflow.com/questions/710609/checking-python-module-version-at-runtime) might help. – dumbmatter Aug 16 '11 at 14:43

2 Answers2

3

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
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
moraes
  • 12,695
  • 7
  • 43
  • 58
  • Thanks @moraes This is exactly what i needed – kamal Aug 16 '11 at 19:40
  • somehow MySQLdb is not working with .__version__ on a command line i can import and use MySQLdb.__version__ but when i use the same syntax in script it does not give me the version number – kamal Aug 16 '11 at 22:02
0

I found it quite unreliable to use the various tools available (including the best one pkg_resources mentioned by moraes' answer), as most of them do not cover all cases. For example

  • built-in modules
  • modules not installed but just added to the python path (by your IDE for example)
  • two versions of the same module available (one in python path superseding the one installed)

Since we needed a reliable way to get the version of any package, module or submodule, I ended up writing getversion. It is quite simple to use:

from getversion import get_module_version
import foo
version, details = get_module_version(foo)

See the documentation for details.

smarie
  • 3,668
  • 20
  • 33