Python
Python Modules
Version Checking
Programming
Software Development

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:

bash
python -m pip show requests

Output:

 
1Name: requests
2Version: 2.31.0
3Summary: Python HTTP for Humans.
4Home-page: https://requests.readthedocs.io
5Author: Kenneth Reitz
6License: Apache 2.0
7Location: /home/user/.venv/lib/python3.11/site-packages
8Requires: charset-normalizer, idna, urllib3, certifi

The Location field shows where the package is installed, which helps when debugging interpreter mismatches.

pip list for all packages

bash
python -m pip list

Output:

 
1Package            Version
2------------------ ---------
3certifi            2023.7.22
4charset-normalizer 3.3.0
5idna               3.4
6pip                23.3.1
7requests           2.31.0
8urllib3            2.0.7

Use --format for machine-readable output, or --outdated to list only packages with newer releases:

bash
1# JSON output for scripting
2python -m pip list --format=json
3
4# Show only outdated packages
5python -m pip list --outdated

importlib.metadata in code (Python 3.8+)

This is the modern, recommended way to read a version from Python code:

python
1from importlib.metadata import version, PackageNotFoundError
2
3try:
4    v = version("requests")
5    print(f"requests version: {v}")
6except PackageNotFoundError:
7    print("requests is not installed")

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:

python
1import requests
2print(requests.__version__)  # 2.31.0
3
4import flask
5print(flask.__version__)     # 3.0.0
6
7import numpy
8print(numpy.__version__)     # 1.26.0

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:

python
1# These can raise AttributeError
2import os
3print(os.__version__)  # AttributeError: module 'os' has no attribute '__version__'
4
5import google.cloud.storage
6print(google.cloud.storage.__version__)  # may or may not exist

pkg_resources (legacy)

Before importlib.metadata, the standard approach was pkg_resources from setuptools:

python
1import pkg_resources
2
3version = pkg_resources.get_distribution("requests").version
4print(version)  # 2.31.0

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:

bash
1python --version               # Python itself
2django-admin --version         # Django
3flask --version                # Flask
4pytest --version               # pytest
5black --version                # Black formatter
6celery --version               # Celery

This is convenient but inconsistent: not every package has a CLI, and the flag varies (--version, -V, a version subcommand).

Comparison of methods

MethodScopeWorks Without ImportPython VersionReliability
pip showSingle packageYes (CLI)AnyHigh
pip listAll packagesYes (CLI)AnyHigh
importlib.metadataSingle packageN/A (code)3.8+High
__version__Single packageNo (must import)AnyMedium (not universal)
pkg_resourcesSingle packageN/A (code)AnyHigh (but slow)
CLI --versionSingle packageYes (CLI)AnyMedium (not universal)

Checking the Python interpreter version

For the interpreter itself rather than a third-party module:

bash
# From the command line
python --version
python3 --version
python
1# From within Python
2import sys
3print(sys.version)           # '3.11.5 (main, Sep 11 2023, 08:19:27)'
4print(sys.version_info)      # sys.version_info(major=3, minor=11, micro=5, ...)
5print(sys.version_info[:2])  # (3, 11) - just major.minor

To require a minimum Python version at runtime (for example 3.9+):

python
1import sys
2
3if sys.version_info < (3, 9):
4    raise RuntimeError("Python 3.9 or higher is required")

Comparing versions in code

When a feature depends on a specific version, compare with packaging.version:

python
1from importlib.metadata import version
2from packaging.version import Version
3
4installed = Version(version("requests"))
5
6if installed >= Version("2.28.0"):
7    # Use the newer API
8    pass
9else:
10    # Fall back to the older API
11    pass

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:

bash
1# Generate a requirements file
2python -m pip freeze > requirements.txt
3
4# Install from a requirements file
5python -m pip install -r requirements.txt

pip freeze prints a pinned list:

 
1certifi==2023.7.22
2charset-normalizer==3.3.0
3requests==2.31.0
4urllib3==2.0.7

For more robust dependency management, pip-tools, poetry or pdm handle transitive dependencies and lock files.

Checking the wrong environment

  • A bare pip command may belong to a different interpreter than the one you run. Use python -m pip so 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).

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms