49

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

Kara
  • 6,115
  • 16
  • 50
  • 57
Joe Schmoe
  • 1,815
  • 3
  • 15
  • 14
  • 5
    Related: [Checking Python module version at runtime](http://stackoverflow.com/questions/710609/checking-python-module-version-at-runtime) – ire_and_curses Aug 19 '10 at 17:00

6 Answers6

97

Use pkg_resources(part of setuptools). Anything installed from PyPI at least has a version number. No extra package/module is needed.

>>> import pkg_resources
>>> pkg_resources.get_distribution("simplegist").version
'0.3.2'
softvar
  • 17,917
  • 12
  • 55
  • 76
  • pkg_resources import is very slow, the recommended way since Python 3.8 is via `importlib.metadata.version` as outlined above – rth Feb 22 '22 at 11:49
40

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

Nick T
  • 25,754
  • 12
  • 83
  • 121
  • 11
    Just to note that `__version__` is the preferred standard for new code, as recommended by [PEP8](http://www.python.org/dev/peps/pep-0008/). See [Standard way to embed version into Python package?](http://stackoverflow.com/questions/458550/standard-way-to-embed-version-into-python-package) – ire_and_curses Aug 19 '10 at 17:07
  • 3
    `print(YOURMODULE.__version__)` – Gabriel Fair Dec 08 '18 at 03:30
  • How do I do this from REPL without actually doing an import? – Frak Apr 13 '20 at 17:58
  • 1
    It seems like it only works for packages that define `__version__`, no? – Dr_Zaszuś Mar 17 '21 at 13:03
36

Starting Python 3.8, importlib.metadata can be used as a replacement for pkg_resources to extract the version of third-party packages installed via tools such as pip:

from importlib.metadata import version
version('wheel')
# '0.33.4'
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
7

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__.

Matthew J Morrison
  • 4,343
  • 3
  • 28
  • 45
1

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)
phzx_munki
  • 1,058
  • 11
  • 16
  • 2
    I'm getting `AttributeError: module 'pip' has no attribute 'get_installed_distributions'`. Same result in Jupyter 5.0.0 and VS Code 1.30.2 using pip version 18.1. Any suggestions? – Karl Baker Jan 18 '19 at 00:09
  • Sorry: looks like they changed the internal APIs of pip. It was not a supported use, anyway. https://github.com/pypa/pip/issues/5154 – phzx_munki Sep 26 '19 at 19:53
-2
 import sys
 import matplotlib as plt
 import pandas as pd
 import sklearn as skl
 import seaborn as sns

 print(sys.version)
 print(plt.__version__)
 print(pd.__version__)
 print(skl.__version__)
 print(sns.__version__)

The above code shows versions of respective modules: Sample O/P:

3.7.1rc1 (v3.7.1rc1:2064bcf6ce, Sep 26 2018, 14:21:39) [MSC v.1914 32 bit (Intel)] 3.1.0 0.24.2 0.21.2 0.9.0 (sys shows version of python )

aastha
  • 1
  • 2