Skip to content

Instantly share code, notes, and snippets.

@alonho
Created November 29, 2012 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alonho/4168689 to your computer and use it in GitHub Desktop.
Save alonho/4168689 to your computer and use it in GitHub Desktop.
get the version of the current module
import os
import pkg_resources
def get_distribution(package):
'''
Can be called from within eggs in order to get the current distribution:
get_distribution(__package__)
__package__ is the same as __name__ except for script execution where __name__ equals '__main__'
'''
provider = pkg_resources.get_provider(package)
if not hasattr(provider, 'egg_root'): # running from source
return None
metadata = pkg_resources.FileMetadata(os.path.join(provider.egg_root, 'EGG-INFO'))
return pkg_resources.Distribution.from_filename(provider.egg_root, metadata=metadata)
def get_version(package):
dist = get_distribution(package)
return None if dist is None else dist.version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment