How do I check the versions of Python modules?
Categories:
How to Check Python Module Versions

Learn various methods to accurately determine the installed versions of your Python modules, from simple command-line tools to programmatic approaches.
Managing Python environments often requires knowing the exact versions of installed modules. Whether you're debugging, ensuring compatibility, or documenting your project's dependencies, checking module versions is a fundamental skill. This article explores several effective methods, ranging from quick command-line checks to more detailed programmatic inspections, helping you maintain a robust and predictable development setup.
Using pip
for Installed Packages
The pip
package installer is the most common tool for managing Python packages. It provides straightforward commands to list all installed packages and their versions, or to check a specific package. This method is ideal for a quick overview of your environment.
pip list
List all installed Python packages and their versions.
The pip list
command outputs a table of all installed packages, including their versions. If you need to find a specific package, you can filter the output using grep
(on Linux/macOS) or findstr
(on Windows).
pip list | grep requests
# On Windows:
pip list | findstr requests
Filter pip list
output for a specific package, e.g., 'requests'.
pip show <package_name>
.pip show requests
Display detailed information about the 'requests' package.
Programmatic Checks within Python
Sometimes, you need to check a module's version directly within your Python code. This is particularly useful for conditional logic based on version numbers or for logging environment details. Most modules expose their version information through special attributes like __version__
or version
.
import sys
import platform
print(f"Python Version: {sys.version}")
print(f"Python Version (platform): {platform.python_version()}")
try:
import requests
print(f"Requests Version: {requests.__version__}")
except ImportError:
print("Requests module not found.")
try:
import pandas
print(f"Pandas Version: {pandas.__version__}")
except ImportError:
print("Pandas module not found.")
try:
import numpy
print(f"NumPy Version: {numpy.version.version}")
except ImportError:
print("NumPy module not found.")
Programmatically check Python and module versions.
As shown in the example, the attribute name for version information can vary (__version__
, version.version
, etc.). It's good practice to check the module's documentation if __version__
doesn't work.
flowchart TD A[Start] --> B{Need to check module version?} B -->|Yes, quick check| C[Use `pip list` or `pip show`] B -->|Yes, in code| D[Import module] D --> E{Module has `__version__`?} E -->|Yes| F[Access `module.__version__`] E -->|No| G{Check module documentation for version attribute} G --> H[Access `module.version` or similar] C --> I[End] F --> I H --> I
Decision flow for checking Python module versions.
Using pkg_resources
(Legacy) or importlib.metadata
(Modern)
For more robust programmatic version checking, especially when __version__
is not available or you need to query arbitrary installed packages, Python offers dedicated modules. pkg_resources
is part of setuptools
and has been a long-standing solution, while importlib.metadata
(introduced in Python 3.8 and backported for older versions) is the modern, preferred approach.
Python 3.8+ (importlib.metadata
)
from importlib.metadata import version, PackageNotFoundError
try: requests_version = version('requests') print(f"Requests Version (importlib.metadata): {requests_version}") except PackageNotFoundError: print("Requests module not found using importlib.metadata.")
try: numpy_version = version('numpy') print(f"NumPy Version (importlib.metadata): {numpy_version}") except PackageNotFoundError: print("NumPy module not found using importlib.metadata.")
Legacy (pkg_resources
)
try: import pkg_resources requests_version = pkg_resources.get_distribution('requests').version print(f"Requests Version (pkg_resources): {requests_version}") except pkg_resources.DistributionNotFound: print("Requests module not found using pkg_resources.")
try: import pkg_resources numpy_version = pkg_resources.get_distribution('numpy').version print(f"NumPy Version (pkg_resources): {numpy_version}") except pkg_resources.DistributionNotFound: print("NumPy module not found using pkg_resources.")
importlib_metadata
as a backport: pip install importlib_metadata
.Both pkg_resources
and importlib.metadata
provide a reliable way to query package versions by name, even if the package hasn't been imported yet. importlib.metadata
is generally preferred for its cleaner API and better performance.