pip install -r requirements.txt: Install, Create and Fix Errors

Running pip install from a requirements txt file takes one command: activate your virtual environment in the project folder, then run pip install -r requirements.txt. pip reads every line and installs those packages and versions:

python -m venv .venv
.venv\Scripts\activate          # macOS/Linux: source .venv/bin/activate
pip install -r requirements.txt

Every command here really ran on Python 3.12.5 in Windows Command Prompt, installing two small packages from PyPI into a fresh virtual environment.

I’ll show what goes in the file, how to install it, how to create one with pip freeze, version specifiers, the errors you’re likely to hit, and the faster uv option.

What is a requirements.txt file?

It’s a plain text file listing the packages your project needs, one per line, usually with versions. Anyone can then recreate your setup with a single command instead of guessing what to install.

LineMeaning
requestsAny version (latest at install time)
requests==2.32.3Exactly this version
requests>=2.31This version or newer
requests>=2.31,<3At least 2.31 but below 3.0
requests~=2.32.0Compatible release: 2.32.x only
# commentIgnored by pip
-r base.txtInclude another requirements file

How to install requirements.txt with pip

Create a virtual environment first so the packages don’t land in your global Python. Then point pip at the file with -r:

# requirements.txt for the store-report project
tabulate==0.9.0        # exact version
colorama>=0.4.6,<0.5   # any 0.4.x from 0.4.6 up

Output:

Collecting tabulate==0.9.0 (from -r requirements.txt (line 2))
  Using cached tabulate-0.9.0-py3-none-any.whl.metadata (34 kB)
Collecting colorama<0.5,>=0.4.6 (from -r requirements.txt (line 3))
  Using cached colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB)
Using cached tabulate-0.9.0-py3-none-any.whl (35 kB)
Using cached colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Installing collected packages: tabulate, colorama
Successfully installed colorama-0.4.6 tabulate-0.9.0
Package  Version
-------- -------
colorama 0.4.6
pip      24.2
tabulate 0.9.0
Command Prompt output of pip install -r requirements.txt inside a new Python virtual environment, followed by pip list
pip read both lines, picked versions that match, and pip list confirms what’s installed.

Using python -m pip instead of plain pip guarantees you’re installing into the Python you think you are. Inside an activated venv both are the same.

Need just one package at a particular version? See pip install a specific version. And for more on environments, Python virtual environments.

Creating a requirements.txt file with pip freeze

Once your project works, pip freeze lists every installed package with its exact version. Redirect it to a file and you’ve captured the environment:

from colorama import Fore, Style, just_fix_windows_console
from tabulate import tabulate

just_fix_windows_console()
rows = [["Austin", 1250.50], ["Denver", 980.25], ["Boston", 1430.00]]
print(Fore.GREEN + "Store sales" + Style.RESET_ALL)
print(tabulate(rows, headers=["City", "Sales ($)"], floatfmt=",.2f"))

Output:

Store sales
City      Sales ($)
------  -----------
Austin     1,250.50
Denver       980.25
Boston     1,430.00
colorama==0.4.6
tabulate==0.9.0
Command Prompt output of a Python report using tabulate and colorama, then pip freeze writing requirements-lock.txt
The script works, and pip freeze writes exact == pins for everything installed.

I keep two files on bigger projects: a short requirements.txt with the packages I chose, and a frozen lock file with exact versions for deploys.

Why does pip install -r requirements.txt fail?

Two errors cover most failures: a wrong file name or folder, and a version that doesn’t exist. Both stop pip before it installs anything:

tabulate==0.9.0
colorama==9.9.9

Output:

ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirments.txt'
Requirement already satisfied: tabulate==0.9.0 in c:\pyguides\.reqenv\lib\site-packages (from -r requirements-bad.txt (line 1)) (0.9.0)
ERROR: Ignored the following yanked versions: 0.4.2
ERROR: Could not find a version that satisfies the requirement colorama==9.9.9 (from versions: 0.1, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.1.10, 0.1.11, 0.1.12, 0.1.13, 0.1.14, 0.1.15, 0.1.16, 0.1.17, 0.1.18, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2.5, 0.2.6, 0.2.7, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3.9, 0.4.0, 0.4.1, 0.4.3, 0.4.4, 0.4.5rc1, 0.4.5, 0.4.6rc1, 0.4.6)
ERROR: No matching distribution found for colorama==9.9.9
Command Prompt output of pip failing with could not open requirements file and no matching distribution for a version
A typo in the file name, then a pinned version that isn’t on PyPI.
ErrorFix
Could not open requirements filecd to the folder that holds the file, or pass the full path
No matching distribution foundCheck the version on pypi.org or loosen the pin
Failed building wheelUpgrade pip, or use a Python version the package supports
'pip' is not recognizedUse python -m pip or py -m pip
Installs, but ModuleNotFoundErrorYou installed into a different Python; activate the venv first

That last one has a full guide: ModuleNotFoundError after pip install.

Installing requirements.txt faster with uv

uv reads the same file format and installs much faster than pip. Its pip subcommand is a near drop-in replacement:

tabulate==0.9.0
colorama>=0.4.6,<0.5

Output:

Using Python 3.12.5 environment at: .reqenv
Checked 2 packages in 679ms
Using Python 3.12.5 environment at: .reqenv
Checked 3 packages in 5ms
All installed packages are compatible
Command Prompt output of uv pip install -r requirements.txt auditing packages already installed in a virtual environment
uv saw that both packages were already satisfied and finished almost instantly.

I switched to uv for new projects, but plain pip is everywhere, so it’s worth knowing both. Setting up several Pythons? Here’s how to install multiple Python versions.

Useful next reads for managing projects:

Frequently asked questions

How do I install from requirements.txt?

Activate your virtual environment, cd to the project folder, and run pip install -r requirements.txt.

How do I create a requirements.txt file?

Run pip freeze > requirements.txt in your project’s virtual environment, or write the file by hand with the packages you use.

Should I pin exact versions?

For apps you deploy, yes: exact pins make installs repeatable. For libraries, use ranges so they work alongside other packages.

How do I update packages in requirements.txt?

Run pip install -r requirements.txt --upgrade, test, then refresh the file with pip freeze.

Can I install requirements.txt without a virtual environment?

You can, but it mixes packages across projects, and on recent Ubuntu and Homebrew Python it fails with externally-managed-environment.

Where is the requirements file format documented?

See the pip requirements file format reference.