62

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

1

6 Answers 6

107

For Python version < 3.8

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'

For Python versions >= 3.8

importlib.metadata can be used as a replacement for pkg_resources. No extra package/module is needed.

from importlib.metadata import version
version('json2html')
# '1.2.3'
Sign up to request clarification or add additional context in comments.

1 Comment

pkg_resources import is very slow, the recommended way since Python 3.8 is via importlib.metadata.version as outlined above
62

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'

2 Comments

It now is from importlib_metadata import version for me --- so underscore instead of dot.
@OnceUponATime I believe that's what you need when running python 3.7 or below, see this answer: stackoverflow.com/a/54869712/169947
43

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

4 Comments

Just to note that __version__ is the preferred standard for new code, as recommended by PEP8. See Standard way to embed version into Python package?
print(YOURMODULE.__version__)
How do I do this from REPL without actually doing an import?
It seems like it only works for packages that define __version__, no?
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__.

3 Comments

django also has django.get_version(), which returns a string rather than a tuple. When in doubt, dir(module).
Or you can use the getmembers function from the inspect module.
The above works for mock too. No luck with .get_distribution("xxx").version()
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)

2 Comments

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?
Sorry: looks like they changed the internal APIs of pip. It was not a supported use, anyway. github.com/pypa/pip/issues/5154
-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 )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.