Skip to content

Module 14 · Python Modules and Packages — topic 10 of 13

pip

The pip package installer: install, uninstall, list, show and freeze, version specifiers, upgrading pip itself, installing from a requirements file, and reading package metadata.

Beginner 8 min read·Lesson 218 of 306 in Python Programming

Learning objectives

  • Use pip's main commands
  • Read and write version specifiers
  • Inspect installed packages

Definition

pip is Python's standard package installer. It downloads packages and their dependencies from PyPI (or other sources) and installs them into the current environment, and it can list, inspect and remove installed packages.

Introduction

pip is the tool you'll use most when working with third-party code. A handful of commands cover nearly everything.

Why it matters

Fluency with pip lets you set up projects quickly, reproduce environments and troubleshoot version conflicts.

Detailed explanation

pip install name installs a package plus everything it depends on; pip uninstall name removes it (dependencies stay).

pip list shows installed packages and versions; pip list --outdated shows available upgrades.

pip show name prints details: version, summary, location and what it requires.

pip freeze prints installed packages as name==version lines, ready for a requirements file.

Version specifiers: ==2.32.3 (exact), >=2.0 (minimum), <3 (below), ~=2.2 (compatible release, 2.2 or later but below 3), and combinations such as >=2.2,<3.

pip install -r requirements.txt installs everything listed in a file.

Keep pip itself up to date with python -m pip install --upgrade pip.

When two packages need incompatible versions of the same dependency, pip's resolver reports a conflict instead of installing a broken set. The fix is usually to relax a pin, upgrade one of the packages, or keep the projects in separate virtual environments.

Syntax

Terminal
python -m pip list
python -m pip show requests
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
  • list, show and freeze inspect.
  • install/uninstall change the environment.
  • -r installs from a file.

Code examples

Inspecting installed packages

Terminal
python -m pip list
python -m pip show requests
python -m pip list --outdated

The output depends on what is installed on your machine.

Version specifiers

Terminal
python -m pip install "requests==2.32.3" # exactly this version
python -m pip install "numpy>=2.0" # at least 2.0
python -m pip install "pandas>=2.2,<3" # 2.2 up to (not including) 3
python -m pip install "rich~=13.7" # compatible with 13.7

More examples

Reading requirement specifiers

Python
import re
reqs = ["requests==2.32.3", "numpy>=2.0", "rich", "pandas<3,>=2.2"]
for r in reqs:
name, spec = re.match(r"([A-Za-z0-9_.-]+)(.*)", r).groups()
print(f"{name:<9}{spec or 'any version'}")
Output
requests ==2.32.3
numpy    >=2.0
rich     any version
pandas   <3,>=2.2

Step-by-step explanation

  • The regular expression splits each requirement into a package name and the rest.
  • An empty specifier means pip may choose any version (normally the latest).
  • Real tools use the packaging library to parse specifiers fully.

Expected output

Running “Reading requirement specifiers” prints exactly:

TextOutput
requests ==2.32.3
numpy >=2.0
rich any version
pandas <3,>=2.2

Practical applications

  • Setting up a project on a new computer with pip install -r.
  • Investigating which package pulled in an unexpected dependency with pip show.

Best practices

  • Use python -m pip.
  • Pin versions for applications.
  • Freeze from a clean virtual environment.

Common mistakes

Freezing a global environment

Avoid

pip freeze > requirements.txt # includes every package on the system

Prefer

Activate the project's virtual environment first, then freeze.

Otherwise the file lists unrelated packages.

Important notes

  • pip check reports installed packages with incompatible dependencies.

Key takeaways

  • pip installs, removes and inspects packages.
  • Specifiers control acceptable versions.
  • freeze and -r reproduce environments.

Try it yourself

  1. 1
    Explain what pip install "django~=5.1" allows.
  2. 2
    Extend the parser example to report whether each requirement is pinned exactly.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

5 questions cover the “Python Modules and Packages” module. Answers and explanations are revealed after you submit.

  • ModerateWhat happens the second time a module is imported in the same program?
  • ModerateCode under if __name__ == "__main__": runs when…
  • ModerateWhat is printed?
Practise this module