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

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'

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


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'

Tags:

Python