Skip to content

Instantly share code, notes, and snippets.

@adamv
Created January 15, 2010 00:32
Show Gist options
  • Save adamv/277656 to your computer and use it in GitHub Desktop.
Save adamv/277656 to your computer and use it in GitHub Desktop.
Function to determine the version of a Python module.
def check_module_version(module_name, version_finder=None):
print module_name+':',
try:
module = __import__(module_name)
if callable(version_finder):
version = version_finder(module)
else:
try:
version = getattr(module, '__version__')
except AttributeError:
try:
version = getattr(module, 'version')
except AttributeError:
version = '(unknown version)'
print version
except ImportError:
print "not found."
check_module_version('memcache')
check_module_version('MySQLdb')
check_module_version('oursql')
check_module_version('pymongo')
check_module_version('mod_python')
check_module_version('django', lambda x: x.get_version())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment