Skip to content Skip to sidebar Skip to footer

How Do I Get The Version Of An Installed Module In Python Programatically

for the modules: required_modules = ['nose', 'coverage', 'webunit', 'MySQLdb', 'pgdb', 'memcache'] and programs: required_programs = ['psql', 'mysql', 'gpsd', 'sox', 'memcached'

Solution 1:

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 record of the installed version. Then you can do:

importpkg_resourcesversion= pkg_resources.get_distribution("nose").version

Solution 2:

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.

Post a Comment for "How Do I Get The Version Of An Installed Module In Python Programatically"