Python
Pip
Package Management
Programming
Version Control

Find which version of package is installed with pip

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

The fastest way to check which version of a Python package is installed is python -m pip show package-name. The Version: line in the output gives you the exact installed version. The critical detail most developers miss is that you must run this against the correct Python interpreter, because different virtual environments, system installs, and conda environments each maintain their own independent package versions.

pip show for a Single Package

This is the primary command for checking one specific package:

bash
python -m pip show requests

Output:

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

Several fields here are useful beyond just the version:

  • Location tells you which environment the package lives in. If this points to a system path rather than your virtualenv, you are checking the wrong Python.
  • Requires lists direct dependencies of this package.
  • Required-by shows which installed packages depend on this one, which is important before upgrading or removing.

Why python -m pip instead of bare pip

Running python -m pip guarantees that pip executes under the interpreter you specified. A bare pip command might resolve to a different Python installation, especially on systems with Python 2 and Python 3 coexisting or when a virtualenv is not activated.

bash
1# These might point to different Pythons
2which pip        # /usr/bin/pip (system Python 3.9)
3which python     # /home/user/.venv/bin/python (venv Python 3.12)
4
5# This always matches the interpreter you name
6python -m pip show requests    # checks the venv
7python3.9 -m pip show requests # checks system Python 3.9

pip list for Browsing the Environment

When you want to see all installed packages or are not sure of the exact package name:

bash
python -m pip list

Output (truncated):

text
1Package            Version
2------------------ ---------
3certifi            2024.8.30
4charset-normalizer 3.3.2
5idna               3.7
6pip                24.2
7requests           2.32.3
8urllib3             2.2.2

Filtering with grep

Combine with grep to search for a specific package or pattern:

bash
python -m pip list | grep -i requests

Output formats

pip list supports several formats useful for scripting:

bash
1# Freeze format (matches requirements.txt syntax)
2python -m pip list --format=freeze
3
4# JSON for programmatic processing
5python -m pip list --format=json
6
7# Show only outdated packages
8python -m pip list --outdated

The --outdated flag is particularly useful for maintenance. It shows the installed version alongside the latest available version:

bash
python -m pip list --outdated
text
1Package    Version  Latest   Type
2---------- -------- -------- -----
3certifi    2024.2.2 2024.8.30 wheel
4requests   2.31.0   2.32.3    wheel

pip freeze for Reproducible Dependency Snapshots

pip freeze outputs installed packages in a format directly usable as a requirements.txt file:

bash
python -m pip freeze
text
1certifi==2024.8.30
2charset-normalizer==3.3.2
3idna==3.7
4requests==2.32.3
5urllib3==2.2.2

To check a specific package:

bash
python -m pip freeze | grep -i requests

The difference between pip list and pip freeze is intent. list is for human browsing. freeze is for generating pinned dependency files. freeze excludes pip itself and other installer packages by default.

Checking the Version From Inside Python

Sometimes you need to verify the version from within a running script or notebook, not from the command line. Python's importlib.metadata module (available since Python 3.8) handles this:

python
from importlib.metadata import version

print(version("requests"))  # Output: 2.32.3

For older Python versions, the pkg_resources module works but is slower and heavier:

python
import pkg_resources

print(pkg_resources.get_distribution("requests").version)

Many packages also expose their version as a module attribute:

python
1import requests
2print(requests.__version__)  # Output: 2.32.3
3
4import flask
5print(flask.__version__)

Not every package exposes __version__, so importlib.metadata is the reliable universal approach.

Package Name vs Import Name

A common source of confusion is that the name you install with pip is not always the name you import in Python. These are the distribution name and the import name, and they can differ:

pip install namePython import namepip show uses
beautifulsoup4bs4beautifulsoup4
PillowPILPillow
scikit-learnsklearnscikit-learn
python-dateutildateutilpython-dateutil
PyYAMLyamlPyYAML
opencv-pythoncv2opencv-python

If pip show bs4 returns nothing, try pip show beautifulsoup4. The pip show command always uses the distribution name (the name you use with pip install).

Verifying the Correct Environment

The single most common source of version confusion is checking the wrong environment. Here is a systematic way to verify:

bash
1# Step 1: Confirm which Python you are using
2which python          # macOS/Linux
3where python          # Windows
4
5# Step 2: Confirm which pip matches that Python
6python -m pip --version
7
8# Step 3: Check the package version
9python -m pip show requests
10
11# Step 4: Confirm from inside Python
12python -c "from importlib.metadata import version; print(version('requests'))"

If you are using a virtual environment, make sure it is activated first:

bash
1# macOS/Linux
2source .venv/bin/activate
3python -m pip show requests
4
5# Windows
6.venv\Scripts\activate
7python -m pip show requests

For conda environments:

bash
1conda activate myenv
2python -m pip show requests
3# or use conda's own command
4conda list requests

Common Pitfalls

Checking the wrong Python interpreter. Running pip show against system Python while your project uses a virtualenv gives you the wrong version. Always use python -m pip with the same interpreter your application runs.

Confusing import names with distribution names. pip show sklearn fails because the distribution is named scikit-learn. Check the project's installation docs or use pip list to find the correct distribution name.

Trusting __version__ unconditionally. Not every package exposes this attribute, and some packages set it incorrectly in edge cases (like editable installs with stale metadata). importlib.metadata.version() reads from installed package metadata and is more reliable.

Using bare pip on systems with multiple Python versions. The pip command might resolve to Python 3.9 while your project requires Python 3.12. Always qualify with python -m pip or python3.12 -m pip.

Forgetting that editable installs can show stale versions. If a package is installed with pip install -e . (editable mode), pip show displays the version from the package metadata, which might not reflect uncommitted changes to the source.

Not checking Required-by before upgrading. Upgrading one package can break others that depend on a specific version range. Check pip show package-name and review the Required-by field before running pip install --upgrade.

Summary

  • Use python -m pip show package-name for the clearest single-package version check, including location and dependency information.
  • Use python -m pip list to browse all packages in the environment, with --outdated to find upgradeable packages.
  • Use python -m pip freeze for generating pinned dependency files for reproducibility.
  • Use importlib.metadata.version("package") to check versions from within running Python code.
  • Always verify you are checking the correct Python interpreter and environment before trusting version output.
  • Remember that distribution names (pip install) and import names (Python import) can differ for many popular packages.

Related reading
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
Track 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.

Browse interview questions

All Rights Reserved.