How do I get a python module's version number through code?

0 votes
asked Aug 19, 2010 by joe-schmoe

I'm trying to get the version number of a specific few modules that I use. Something that I can store in a variable.

4 Answers

0 votes
answered Aug 19, 2010 by matthew-j-morrison

I think it depends on the module. For example, Django has a VERSION variable that you can get from django.VERSION, sqlalchemy has a __version__ variable that you can get from sqlalchemy.__version__.

0 votes
answered Aug 19, 2010 by nick-t

Generalized answer from Matt's, do a dir(YOURMODULE) and look for __version__, VERSION, or version. Most modules like __version__ but I think numpy uses version.version

0 votes
answered Aug 20, 2013 by softvar

Use pkg_resources. Anything installed from PyPI at least have a version number.

>>> import pkg_resources
>>> pkg_resources.get_distribution("simplegist").version
'0.3.2'
0 votes
answered Sep 15, 2017 by phzx-munki

Some modules (e.g. azure) do not provide a __version__ string.

If the package was installed with pip, the following should work.

# say we want to look for the version of the "azure" module
import pip
for m in pip.get_installed_distributions():
    if m.project_name == 'azure':
        print(m.version)
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...