Version number

November 30, 2023

Use the importlib.metadata library in Python to get the version number of a package. The example below gets the version of the ipython package that has been installed in the environment.

>>> from importlib.metadata import version
>>> version('ipython')
'8.15.0'

In a package, instead of defining __version__ somewhere in the package, the version number can just be defined in the pyproject.toml file as shown below. Then importlib.metadata can be used to get the version number of that package. This approach has been adopted by Flask.

[project]
name = "MyPackage"
version = "3.1"