HOWTO · Python
How to Check Python Module Version
Learn how to check an installed Python package version with importlib.metadata, a module's __version__ attribute, pip show, or pkg_resources.
On this page
To check an installed package’s version from Python, use importlib.metadata.version() with the package’s distribution name. For a terminal check, run python -m pip show PACKAGE with the Python interpreter whose environment you want to inspect. Import names and distribution names can differ, so use the name listed by the package’s installation instructions or package metadata.
Use importlib.metadata to get an installed distribution’s version
importlib.metadata reads metadata for installed distributions. Pass a distribution name such as requests; the name you write in an import statement is not always the same. This example prints the name and version of the first distribution returned by the current environment:
from importlib import metadata
distributions = list(metadata.distributions())
if distributions:
distribution = distributions[0]
print(f"{distribution.metadata['Name']}: {distribution.version}")
else:
print("No installed distributions found.")
pip: 26.2.1
For one known package, call metadata.version("requests"). If the standard-library importlib.metadata module is unavailable in the Python environment, the separately installed backport uses the import path importlib_metadata; its lookup API is also named version(). Availability depends on the environment, so this article does not assume a particular Python-version threshold.
A lookup failure can mean that the distribution is not installed in the active environment, that the name is wrong, or that the command is using a different Python environment from the one where the package was installed. Check the distribution name and interpreter before drawing a conclusion.
Check a module’s __version__ attribute when available
Some modules expose their version as a __version__ string. For example, after importing a module that provides the attribute, read it with print(module.__version__). This is a convenient check when the attribute exists, but it is not part of a universal requirement for Python modules. The standard-library metadata lookup above is a better fit when you need the installed distribution’s metadata by name.
Check a package with pip show
Use python -m pip show PACKAGE in a terminal to display details for one installed distribution. This command example is unexecuted here. For example, run python -m pip show requests. Find the Version line in the result. Running pip as a module of the chosen Python helps align the command with that interpreter’s environment; a bare pip command can refer to a different installation. pip show reports package metadata, and does not install or update the package.
Use pkg_resources for legacy environments
In environments that use pkg_resources, import it before calling get_distribution(). Pass a distribution name, which may differ from the module’s import name. This standalone example checks the installed pip distribution when pkg_resources is available; the API may be absent from some environments:
try:
import pkg_resources
except ImportError:
print("pkg_resources is unavailable in this environment.")
else:
try:
print(pkg_resources.get_distribution("pip").version)
except pkg_resources.DistributionNotFound:
print("pip is not installed in this environment.")
This is a legacy alternative; for new code, prefer importlib.metadata where it is available.