How do you check which version of a package is installed using pip? | A1Lab@MZeeshanM to PythonEnglish • 4 days ago

How do you check which version of a package is installed using pip?

message-square
0

Run python -m pip show <package-name> in your terminal to inspect the exact installed version, installation path (Location), and dependencies—or run python -m pip list to view all installed packages and their versions at once inside your active Python environment.

Basic Example

# 1. Recommended: Show detailed metadata including the exact Version line
python -m pip show requests
# Output:
# Name: requests
# Version: 2.32.3
# Summary: Python HTTP for Humans.
# Location: /path/to/.venv/lib/python3.12/site-packages
# ...

# 2. List all installed packages and versions in a clean table
python -m pip list

# 3. Filter for a specific package in a list (Linux/macOS vs. Windows)
python -m pip list | grep -i pandas       # Linux / macOS / Git Bash
python -m pip list | findstr /i pandas    # Windows Command Prompt / PowerShell

Key Details & Edge Cases

  • Always prefer python -m pip over bare pip:
    • If multiple Python versions or virtual environments exist on your machine, bare pip show pkg can query a different environment than the python interpreter you run. Prefixing with python -m pip guarantees you inspect the site-packages directory of that exact Python executable.
  • Distribution name vs. import name (pip show uses the PyPI distribution name):
    • Pass the package distribution name rather than the Python import name when they differ—for example, run python -m pip show Pillow (not PIL), python -m pip show scikit-learn (not sklearn), or python -m pip show beautifulsoup4 (not bs4). Note that pip show is case-insensitive (pillow and Pillow both work).
  • Exit codes when a package is missing:
    • python -m pip show nonexistent-pkg prints WARNING: Package(s) not found: nonexistent-pkg and exits with status code 1, making it reliable for shell scripts and CI checks.
  • Machine-readable JSON output (pip list --format=json):
    • When parsing installed versions in automation scripts, run python -m pip list --format=json (or python -m pip inspect in modern pip 22.2+) instead of scraping human-formatted text tables.
  • Checking the version inside Python code (importlib.metadata):
    • If you need the installed version programmatically at runtime without spawning a subprocess, use Python’s standard library importlib.metadata.version("requests") (which works even for packages that lack a __version__ attribute).

Comparison of pip Version Inspection Commands

Command Output Format Checks Single or All Packages? Shows Install Location & Dependencies? Best Used When
python -m pip show <pkg> Key-value metadata block Single (or space-separated list) Yes (Location, Requires, Required-by) Verifying a specific package’s exact version and which site-packages folder loaded it.
python -m pip list Aligned two-column table All installed packages No (Name + Version only) Getting an overview of every library installed in the active environment.
python -m pip list --outdated Table (Version vs. Latest) Outdated packages only No Checking which installed packages have newer releases available on PyPI.
python -m pip freeze package==version lines All installed packages No Generating exact pinned version pins for requirements.txt or lockfiles.
python -m pip list --format=json JSON array of objects All installed packages No Programmatically parsing package names and versions in CI/CD or automation scripts.