Sub topic 8
Recording and Recreating an Environment with requirements.txt
This chapter assumes you can create and activate a virtual environment and install packages with python -m pip. It covers how to save the packages in an environment to a file and how to rebuild the environment from that file.
Why record an environment
A virtual environment lives in a directory that you should not commit or copy to other machines. What you share is a list of the packages it contains. A requirements file is a plain text file with one requirement per line. Anyone with Python can run pip against it and get a matching environment.
Writing the file with pip freeze
pip freeze prints every installed package with its exact version, in a format pip can read back. Redirect the output into a file.
python -m pip freeze > requirements.txt
python3 -m venv .venv-new
.venv-new/bin/python -m pip install -r requirements.txtThe second and third lines show the round trip. They create a fresh environment and install the recorded set into it. Calling the environment's own python directly means you do not need to activate it first.
The file contains lines like these:
certifi==2024.8.30
charset-normalizer==3.4.0
idna==3.10
requests==2.32.3
urllib3==2.2.3Notice that freeze lists everything, including packages you never installed yourself. certifi, idna, and urllib3 are dependencies of requests. This is useful for an exact copy, but it makes the file harder to read.
Two styles of requirements file
You can keep two kinds of files, and they serve different purposes.
| Style | Example line | Written by | Good for |
|---|---|---|---|
| Direct requirements | `requests>=2.31,<3` | You | Stating what the project needs |
| Frozen pins | `requests==2.32.3` | `pip freeze` | Rebuilding the same versions |
A common pattern is to write your direct requirements by hand, then generate a frozen file from a working environment when you need an exact copy. The next chapters cover constraints files, which give you a way to hold exact versions without listing every dependency as a requirement.
Installing from a file
The -r option tells pip to read requirements from a file.
python -m pip install -r requirements.txtpip installs each line in turn, resolving dependencies as it goes. Lines that are already satisfied are skipped. If a pinned version no longer exists on the index, or conflicts with another line, pip stops with an error and names the conflicting requirements.
You can also add comments with # and include one file from another with -r other.txt. Blank lines are ignored.
Common mistakes
- Freezing the wrong interpreter. The file lists unrelated packages. Confirm which environment is active before you freeze.
- Using
pipinstead ofpython -m pip. A barepipcommand can belong to a different interpreter thanpython. - Editing a frozen file by hand and forgetting dependencies. If you remove a top-level package but leave its dependencies, they stay installed on the next rebuild.
- Redirecting to a file you are also reading. Write to
requirements.txtfromfreeze, but never use that file as input in the same command. - Expecting the file to cover Python itself. A requirements file records packages, not the Python version. Record the version in your README or project metadata.
Checking the result
After rebuilding, run python -m pip check in the new environment. It reports installed packages whose dependencies are missing or incompatible. An empty result, "No broken requirements found.", means the set is consistent.
Exercise
You run python -m pip freeze > requirements.txt and the file lists dozens of packages you never installed, including some unrelated to your project. What is the most likely cause, and how do you fix it?
Show answer
You ran the command with the system or base interpreter instead of the project's virtual environment. Activate the environment (or call .venv/bin/python directly), confirm with sys.prefix that it points to the environment, and run the freeze command again.
Exercise
Why does a frozen file list urllib3 even though you only ran python -m pip install requests?
Show answer
urllib3 is a dependency of requests. pip installed it automatically, and pip freeze lists every installed package, not only the ones you requested directly.