How do I check the versions of Python modules?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The fastest way to check a Python module's version is python -m pip show package_name, which prints the version along with other metadata. From within a script, use importlib.metadata.version("package_name") (Python 3.8+). Several other methods exist depending on whether you need to check one package, all packages, or verify versions programmatically at runtime.
Method 1: pip show (Single Package)
This is the most reliable method for checking a specific package:
Output:
The Location field also tells you where the package is installed, which is useful for debugging interpreter mismatches.
Method 2: pip list (All Packages)
To see every installed package and its version:
Output:
For machine-readable output, use the --format flag:
Method 3: importlib.metadata (Programmatic, Python 3.8+)
This is the modern, recommended way to check versions from within Python code:
This works for any installed package, regardless of whether the package exposes a __version__ attribute.
Method 4: __version__ Attribute
Many packages define a __version__ attribute on their top-level module:
This is simple but has a significant limitation: not all packages follow this convention. Some use different attribute names, and some do not expose a version at runtime at all.
Method 5: pkg_resources (Legacy)
The pkg_resources module from setuptools was the standard before importlib.metadata:
This still works but is considered legacy. importlib.metadata is faster (it does not scan all packages on import) and is part of the standard library. Prefer importlib.metadata for new code.
Method 6: CLI Tools from Packages
Some packages provide their own command-line version flag:
This is convenient but inconsistent. Not every package provides a CLI, and the flag name varies (--version, -V, version subcommand).
Comparison of Methods
| Method | Scope | Works Without Import | Python Version | Reliability |
pip show | Single package | Yes (CLI) | Any | High |
pip list | All packages | Yes (CLI) | Any | High |
importlib.metadata | Single package | N/A (code) | 3.8+ | High |
__version__ | Single package | No (must import) | Any | Medium (not universal) |
pkg_resources | Single package | N/A (code) | Any | High (but slow) |
CLI --version | Single package | Yes (CLI) | Any | Medium (not universal) |
Checking Python's Own Version
To check the Python interpreter version (not a third-party module):
For runtime version checks in code (e.g., requiring Python 3.9+):
Programmatic Version Comparison
When you need to check versions at runtime (e.g., to use a feature that was added in a specific version), use packaging.version:
Do not compare version strings directly. String comparison gives wrong results: "2.9.0" > "2.10.0" evaluates to True because "9" > "1" lexicographically.
Freezing and Requirements Files
To capture all installed package versions for reproducibility:
The output of pip freeze is a pinned version list:
For more robust dependency management, consider pip-tools, poetry, or pdm which handle transitive dependencies and lock files.
Common Pitfalls
- Using a bare
pipcommand that belongs to a different Python interpreter. Always usepython -m pipto ensure you are checking the correct environment. - Comparing version strings with
<or>operators."2.9" > "2.10"isTruein Python because it is a lexicographic comparison. Usepackaging.version.Versionfor correct semantic comparison. - Relying on
__version__for packages that do not define it. Useimportlib.metadata.version()instead, which reads the installed package metadata regardless of what the module exposes. - Checking versions in a terminal but running code in a different environment (system Python vs. venv vs. conda). The version you see may not be the version your code imports.
- Using
pkg_resourcesin performance-sensitive code. It scans all installed packages on first import, which adds noticeable startup time.importlib.metadatais significantly faster.
Summary
- For a quick check, use
python -m pip show package_nameto see the version of a specific package orpython -m pip listfor all packages. - In code, use
importlib.metadata.version("package_name")(Python 3.8+). It is reliable, fast, and works for every installed package. - The
__version__attribute works for many popular packages but is not universal. Do not depend on it for arbitrary packages. - Always use
python -m pipinstead of barepipto avoid interpreter mismatches. - Use
packaging.version.Versionfor programmatic version comparison. Never compare version strings lexicographically.
Related reading
- How do I check the versions of Python modules?
- How do I check whether a file exists without exceptions?
- How do I check which version of NumPy I'm using?
- How do I check which version of Python is running my script?
- How do I check which version of Python is running my script?
- How do I clone a Django model instance object and save it to the database?
- How do I clone a list so that it doesn't change unexpectedly after assignment?
- How do I combine two dataframes?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.