How to check the version of a Python module or package
Codemia Knowledge Hub ·
Run python -m pip show package_name to print a package's version and metadata. Inside a script, call importlib.metadata.version("package_name") (Python 3.8+). To see every installed package at once, use python -m pip list.
pip show for one package
This is the most reliable way to check a specific package:
Output:
The Location field shows where the package is installed, which helps when debugging interpreter mismatches.
pip list for all packages
Output:
Use --format for machine-readable output, or --outdated to list only packages with newer releases:
importlib.metadata in code (Python 3.8+)
This is the modern, recommended way to read a version from Python code:
It reads the installed package metadata, so it works for any installed package whether or not the package exposes a __version__ attribute.
The __version__ attribute
Many packages define __version__ on their top-level module:
This is simple, but not every package follows the convention. Some use a different attribute name, and some expose no version at runtime at all:
pkg_resources (legacy)
Before importlib.metadata, the standard approach was pkg_resources from setuptools:
It still works but is legacy. It scans all installed packages on first import, which adds noticeable startup time, while importlib.metadata is faster and part of the standard library. Use importlib.metadata in new code.
A package's own CLI flag
Some packages ship a command-line tool with a version flag:
This is convenient but inconsistent: not every package has a CLI, and the flag varies (--version, -V, a 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 the Python interpreter version
For the interpreter itself rather than a third-party module:
To require a minimum Python version at runtime (for example 3.9+):
Comparing versions in code
When a feature depends on a specific version, compare with packaging.version:
Never compare version strings directly. String comparison is lexicographic, so "2.9.0" > "2.10.0" evaluates to True because "9" > "1".
Pinning versions with pip freeze
To capture every installed version for reproducibility:
pip freeze prints a pinned list:
For more robust dependency management, pip-tools, poetry or pdm handle transitive dependencies and lock files.
Checking the wrong environment
- A bare
pipcommand may belong to a different interpreter than the one you run. Usepython -m pipso you check the right environment. - A version checked in one terminal may not be what your code imports if the code runs in another environment (system Python, a venv or conda).
Related reading
- 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 clone a Django model instance object and save it to the database?
- How do I combine two dataframes?
- How do I compare version numbers in Python?
- How do I concatenate text files in Python?
- How do I configure PyCharm to run py.test tests?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.