Run python -m pip install -r requirements.txt in the folder that contains the file, ideally inside a virtual environment, and pip installs every package listed in it. To create the file from an environment that already works, run python -m pip freeze > requirements.txt, which writes every installed package with its exact version.
How to install from requirements.txt
Create and activate a virtual environment first, so the packages go into the project instead of the system Python:
# Windows (PowerShell)
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txt
# macOS / Linux
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
The -r flag means "read requirements from this file". Without a virtual environment, recent Linux distributions and Homebrew Python refuse with error: externally-managed-environment; see how to fix externally-managed-environment. If activation itself fails, the venv guide covers each shell, including the PowerShell execution-policy error.
Example output from a run with two requirements (pip 24.0, Python 3.12):
Installing collected packages: urllib3, idna, click, charset-normalizer, certifi, requests
Successfully installed certifi-2026.10.23 charset-normalizer-3.5.2 click-8.5.0 idna-3.20 requests-2.32.3 urllib3-2.8.0
The file listed only requests and click; pip added their dependencies automatically.
How to create a requirements.txt file
From a working environment:
python -m pip freeze > requirements.txt
That produced this file from the environment above:
certifi==2026.10.23
charset-normalizer==3.5.2
click==8.5.0
idna==3.20
requests==2.32.3
urllib3==2.8.0
pip freeze captures every installed package at its exact version, including dependencies you never asked for. That is what you want for repeatable installs of an application. The pip user guide describes requirements files as the place to hold the result of pip freeze for exactly this reason. Run it inside the project's virtual environment; run it against a global Python and you get every package on the machine.
If you'd rather keep a short, hand-written list of only your direct dependencies, write it yourself and freeze into a separate lock-style file when you need exact reproducibility.
requirements.txt format and version specifiers
Each line is a requirement, an option, or a comment. These are all valid:
# Comments start with #
requests==2.32.3 # exact version
click>=8.1,<9 # any 8.x from 8.1 up
rich~=13.7 # compatible release: >=13.7, ==13.*
python-dateutil # any version (avoid for apps)
httpx[http2]==0.27.2 # with an optional extra
pywin32==306 ; sys_platform == "win32" # only on Windows
-r base.txt # include another requirements file
-c constraints.txt # apply a constraints file
--index-url ${PIP_INDEX_URL} # environment variable, ${VAR} form only
Rules from the pip file-format reference worth knowing:
- A
#at the start of a line, or after whitespace, starts a comment. - A line ending in
\continues on the next line. - The file is read as UTF-8 unless a
# -*- coding: ... -*-line says otherwise. - Environment variables are expanded only in
${VAR}form.$VARand%VAR%are not supported. This is how you keep a private index token out of the file. - A space instead of an operator is an error.
requests 2.32.3fails withERROR: Invalid requirement: 'requests 2.32.3' (from line 1 of requirements.txt).
Common errors and fixes
Could not open requirements file
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
pip looks for the path relative to your current directory, not the script's. cd into the project folder or pass the path: python -m pip install -r path/to/requirements.txt. Also check the name: requirement.txt (no s) and requirements.txt.txt (Windows hiding the extension) are both common.
No matching distribution found
ERROR: Could not find a version that satisfies the requirement requests==99.0.0 (from versions: 0.2.0, ... 2.34.2)
ERROR: No matching distribution found for requests==99.0.0
The version doesn't exist, or doesn't exist for your Python version or platform. The from versions: list shows what pip can see; pick one from it. When that list says (from versions: none), the package name is wrong (check spelling and hyphens), you are offline, or a custom --index-url doesn't carry it.
Dependency conflicts
If two packages need incompatible versions of a third, pip's resolver stops with an error that starts like this (example from the pip documentation):
ERROR: Cannot install package_coffee==0.44.1 and package_tea==4.3.0 because these package versions have conflicting dependencies.
It then lists which package depends on which version range. The pip documentation's suggested fixes: remove requirements you no longer need, loosen the version pins it names (for example package_coffee>0.44 instead of ==0.44.1), or keep the pin on the package that matters most and leave the other unpinned.
It installs into the wrong Python
If import fails after a "successful" install, pip ran for a different interpreter. Check with python -c "import sys; print(sys.executable)" and always use python -m pip so pip and python are the same installation.
Splitting dev and production requirements
A common layout is a base file plus extras:
# requirements/base.txt
requests==2.32.3
# requirements/dev.txt
-r base.txt
pytest==8.3.3
python -m pip install -r requirements/dev.txt installs both. A nested -r path is resolved relative to the file that contains it, not to your current directory; in a test run pip reported the included file as -r reqs\base.txt even though the command was run from the parent folder.
Constraints files: pin versions without installing
A constraints file has the same format but only controls which version gets installed if something needs that package. The pip user guide is explicit: "Including a package in a constraints file does not trigger installation of the package." Use it to hold one set of tested versions across several projects:
python -m pip install -r requirements.txt -c constraints.txt
Locking down installs with hashes
For deployments, pip can refuse anything whose file hash doesn't match. Each requirement must be pinned with == and followed by one or more hashes, and adding a hash to any line turns on hash-checking for the whole file, including dependencies:
requests==2.32.3 \
--hash=sha256:70761cfe03c773ceb22aa2f671b4757976145175cdfca038c02654d061d6dcc6
Get a hash for a downloaded file with python -m pip hash requests-2.32.3-py3-none-any.whl, which printed the line above in a test run. To check a file you downloaded manually against a published SHA-256, the checksum tool computes it in the browser.
Frequently asked questions
- Should I use pip or python -m pip?
- The pip documentation uses python -m pip throughout. It guarantees that packages go into the Python you named, which matters when several Pythons or virtual environments are installed. Inside an activated virtual environment, plain pip points at the same interpreter, so either works.
- Does pip install packages in the order listed in requirements.txt?
- Do not rely on it. The pip user guide says a requirements file is just a list of pip install arguments and that you should not depend on items being installed in any particular order. pip's resolver works out the full set of packages and versions first.
- How do I update all packages in requirements.txt?
- If the file pins exact versions with ==, change the pins (or regenerate them) and run pip install -r requirements.txt again. If it uses ranges such as >=, run python -m pip install --upgrade -r requirements.txt to move to the newest versions the ranges allow, then test and re-freeze.
- Can a requirements file include another one?
- Yes. A line such as -r base.txt pulls in another requirements file, and -c constraints.txt applies a constraints file. Relative paths are resolved from the location of the file that contains the line, so reqs/dev.txt can include base.txt from the same folder.