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.
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
- list, show and freeze inspect.
- install/uninstall change the environment.
- -r installs from a file.
Code examples
Inspecting installed packages
The output depends on what is installed on your machine.
Version specifiers
More examples
Reading requirement specifiers
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:
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
Prefer
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
- 1Explain what pip install "django~=5.1" allows.
- 2Extend 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?