Python Modules, Packages and Environments · Lesson 115 of 149

Python pip and requirements.txt

What you will learn

  • What PyPI and pip are
  • Installing, upgrading, listing, showing and uninstalling packages
  • Version specifiers: ==, >=, <
  • Dependencies of packages
  • pip freeze and requirements.txt
  • Safety when choosing packages

1. PyPI and pip

Beyond the standard library there is a huge collection of packages written by others, published on the Python Package Index (PyPI) at pypi.org. pip is the tool that downloads a package from PyPI and installs it into the current Python or environment.

Always run it as python -m pip, so that the package goes to the same Python that will run your code (lessons 16.4 and 16.5). Use it inside a virtual environment, so that it does not clutter the main Python. The examples below were run in a fresh environment with pip 25.2 on Python 3.13.

2. Installing

python -m pip install tabulate

pip downloads the package (and anything it needs) and reports what it installed:

Successfully installed tabulate-0.10.0

Then use it like any module:

import tabulate

rows = [["apple", 3, 1.5], ["pear", 10, 0.25]]
print(tabulate.tabulate(rows, headers=["item", "qty", "price"]))
item      qty    price
------  -----  -------
apple       3     1.5
pear       10     0.25

Notes:

  • The install name and the import name can differ: the package beautifulsoup4 is imported as bs4, and scikit-learn as sklearn. Check the package's page on PyPI.
  • A typo in the name gives a clear error:
ERROR: Could not find a version that satisfies the requirement nosuchpackagexyz123 (from versions: none)
ERROR: No matching distribution found for nosuchpackagexyz123
  • Anyone can publish to PyPI, so be careful with names that look like popular ones (see section 8).
Quick check: why `python -m pip install x` and not just `pip install x`?

python -m pip is certain to use the pip belonging to that python, so the package lands where your code will look for it.

3. Managing packages

Task Command
list installed packages python -m pip list
show details of one python -m pip show requests
list packages with newer versions python -m pip list --outdated
upgrade one python -m pip install --upgrade tabulate
remove one python -m pip uninstall tabulate
check that dependencies fit together python -m pip check

Example outputs from the check environment:

> python -m pip list --outdated
Package  Version Latest Type
-------- ------- ------ -----
pip      25.2    26.2.2 wheel
tabulate 0.9.0   0.10.0 wheel
> python -m pip show requests
Name: requests
Version: 2.34.2
Requires: certifi, charset_normalizer, idna, urllib3
Required-by:
> python -m pip check
No broken requirements found.

(The version numbers are those available when the lessons were checked; yours will be newer.) Upgrading pip itself is python -m pip install --upgrade pip.

4. Choosing versions

You can ask for a specific version or a range with version specifiers:

Specifier Meaning
tabulate the latest version
tabulate==0.9.0 exactly this version
tabulate>=0.8 this version or newer
tabulate>=0.8,<0.9 at least 0.8 and below 0.9
tabulate~=0.8.0 "compatible": 0.8.x, not 0.9

Quote specifiers that contain < or > on the command line, or the shell treats them as redirections:

python -m pip install "tabulate>=0.8,<0.9"

This installed tabulate-0.8.10 in the check (replacing the version that was there). Pinning an exact version (==) gives repeatable results; ranges give you automatic bug fixes but allow surprises.

5. Dependencies

Packages often use other packages. Installing requests brought four more along:

Successfully installed certifi-2026.10.19 charset_normalizer-3.5.2 idna-3.20 requests-2.34.2 urllib3-2.8.0

pip show listed them under Requires. Those extra packages are dependencies (or "transitive" dependencies, when they come in through another package). Your project directly needs only requests; the others are needed by requests. Uninstalling requests removes only requests, not its dependencies, which stay behind:

> python -m pip uninstall -y requests tabulate  
> python -m pip freeze
certifi==2026.10.19
charset-normalizer==3.5.2
idna==3.20
urllib3==2.8.0

Rebuild the environment instead of cleaning by hand, if you want it tidy.

6. requirements.txt

A requirements file lists the packages a project needs, one per line, so anyone can recreate the environment.

Writing one by hand

# requirements.txt
tabulate==0.9.0
requests>=2.30,<3

Lines starting with # are comments. List the packages you import, with the versions you need. Install them all with:

python -m pip install -r requirements.txt

In the check this installed requests-2.34.2 (the newest version below 3 at that moment) and tabulate-0.9.0, plus the four dependencies of requests.

Generating one: pip freeze

pip freeze prints every installed package with its exact version, in requirements format:

> python -m pip freeze
certifi==2026.10.19
charset-normalizer==3.5.2
idna==3.20
requests==2.34.2
tabulate==0.9.0
urllib3==2.8.0

Save it to a file with python -m pip freeze > requirements.txt. This captures the whole environment, dependencies included, with every version pinned. Compare the two styles:

Hand-written From pip freeze
contents only your direct packages everything installed
versions ranges allowed exact pins
good for showing what the project needs reproducing this exact setup
weakness different machines may get different versions long, mixes your choices with dependencies

Many projects keep both: a short requirements.txt of direct needs, and a pinned file (often requirements-lock.txt) from pip freeze for exact reproduction. Tools such as pip-tools, poetry and uv automate this "lock file" idea.

The usual workflow

  1. Create and enter an environment (lesson 16.5).
  2. python -m pip install what you need.
  3. Record: python -m pip freeze > requirements.txt (or edit by hand).
  4. Commit requirements.txt to git (not the .venv folder).
  5. On another machine: create an environment, then python -m pip install -r requirements.txt.

7. Where packages come from

By default pip downloads from PyPI. It can also install:

  • from a local file or folder (pip install ./mypackage; with -e for an "editable" install that follows your edits, lesson 19.5);
  • from a git repository (pip install git+https://...);
  • from a private index run by a company (--index-url).

Standard library modules are not on PyPI and are never installed with pip.

8. Choosing packages safely

Installing a package means running someone else's code on your computer, and it can run during installation. Habits that reduce risk:

  • Check the exact name and spelling on pypi.org; look-alike names ("typosquatting") exist to catch typing mistakes.
  • Prefer well-known, maintained packages: look at the release history, the project page and the source repository.
  • Use a virtual environment, so mistakes are easy to throw away.
  • Do not run pip install with administrator rights or sudo.
  • Install only what you need, and review new dependencies when you add them.
  • Keep important packages up to date for security fixes (pip list --outdated).

Worked example: reproducing an environment

A project needs tabulate pinned and requests within a range. The requirements file is:

tabulate==0.9.0
# a comment
requests>=2.30,<3

Create an environment and install:

> python -m venv .venv  
> .venv\Scripts\python -m pip install -r requirements.txt
Successfully installed certifi-2026.10.19 charset_normalizer-3.5.2 idna-3.20 requests-2.34.2 tabulate-0.9.0 urllib3-2.8.0

Then freeze the exact state into a lock file for the future:

> .venv\Scripts\python -m pip freeze > requirements-lock.txt

and use the installed package:

import tabulate

rows = [["apple", 3, 1.5], ["pear", 10, 0.25]]
print(tabulate.tabulate(rows, headers=["item", "qty", "price"]))
Step Result
requirements.txt records what the project needs: one pin, one range
install -r six packages installed: the two direct ones plus four dependencies
pip freeze all six with exact versions, for reproducing this setup later
the script works only inside this environment

The commands, versions and outputs are real, from a check run for this lesson; newer versions will be installed today. The table printed is the same as in section 2.

Common mistakes

  • Using plain pip instead of python -m pip.
  • Installing into the main Python, with no environment active.
  • Confusing the install name with the import name.
  • Forgetting to quote > and <.
  • Not recording dependencies, so the project cannot be rebuilt.
  • Committing .venv instead of requirements.txt.
  • Trusting any package by name, without checking.
  • Pinning nothing and then being surprised when a new version breaks the project.

Key takeaways

  • pip installs packages from PyPI; run it as python -m pip, inside a virtual environment.
  • Common commands: install, install --upgrade, uninstall, list, show, list --outdated, check.
  • Specifiers such as ==0.9.0, >=0.8,<0.9 control versions; quote them.
  • Packages bring dependencies; pip freeze lists everything installed with exact versions.
  • Keep a requirements.txt in git and recreate environments with pip install -r.
  • Choose packages carefully; installing means running someone else's code.

Practice

1. Write the command that installs exactly version 2.31.0 of requests.

Show answer

python -m pip install requests==2.31.0

2. Why do you have to quote "tabulate>=0.8,<0.9"?

Show answer

Otherwise the shell treats > and < as redirection operators instead of passing them to pip.

3. What does python -m pip install -r requirements.txt do?

Show answer

It installs every package listed in the file, at the versions it specifies.

4. What is the difference between a hand-written requirements file and one made by pip freeze?

Show answer

Hand-written lists only your direct needs (often with ranges); freeze lists every installed package, dependencies included, with exact versions.

5. You uninstall requests. Are its dependencies removed too?

Show answer

No. Only requests is removed; certifi, idna and the others remain.

6. Name two precautions before installing an unfamiliar package.

Show answer

Check the exact name and project page on PyPI (to avoid look-alike names and check it is maintained), and install it in a virtual environment, not the main Python.