Install dependencies from requirements.txt with pip
Install a project dependency list in the intended environment, understand version constraints and target directories, and distinguish a snapshot from a reproducible lock.
Run python -m pip install -r requirements.txt with the Python interpreter that should receive the packages. The -r option tells pip to read a requirements file; it resolves the combined requirements and their dependencies, rather than simply installing each line independently.
Start with a virtual environment
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
On Windows, create the environment with your selected Python and activate .venv\Scripts\Activate.ps1 in PowerShell or .venv\Scripts\activate.bat in cmd. Activation is optional if you invoke the environment's Python by its full path.
A requirements file can contain exact versions, ranges, or unconstrained names:
requests==2.32.3
flask>=3.0,<4.0
pandas
numpy~=1.26.0
These version numbers illustrate syntax, not current security or compatibility recommendations. Review the packages and constraints for your Python version and platform. Installing packages may execute build code, so the file and its configured sources must be trusted.
Useful installation options
# Put packages in a separate directory.
python -m pip install -r requirements.txt --target=/path/to/libs
# Reinstall packages even when the selected versions are installed.
python -m pip install -r requirements.txt --force-reinstall
# Reduce routine output.
python -m pip install -q -r requirements.txt
A target directory does not automatically become importable; the application or packaging process must put it on the appropriate import path. Normally, pip leaves an installed version alone when it satisfies the request. --force-reinstall is not a substitute for declaring the intended versions.
If an approved mirror is required, select it explicitly with --index-url https://packages.example.com/simple. Use a real trusted HTTPS endpoint and the organization's credential mechanism. A mirror changes the package source; it is not merely a bandwidth setting. Combining indexes can introduce dependency-confusion risks.
Export an environment snapshot
python -m pip freeze > requirements.txt
This overwrites the file with an installed-environment snapshot, including indirect dependencies and excluding some bootstrap packages by default. It does not calculate a cross-platform lock or include artifact hashes. Keep direct dependencies in the project's maintained declaration, and use a suitable locking or repeatable-install workflow when deployment needs stronger reproducibility. Import-scanning tools can miss dynamic imports and need review.
FAQ
What does externally-managed-environment mean?
The Python installation is managed by its distributor. Use a virtual environment for project libraries, or an isolated application installer such as pipx for an appropriate command-line tool. Avoid forcing changes into the system environment.
Why does a dependency fail to build?
Read the first relevant build error. Possible causes include missing native headers or compilers, unsupported Python/platform combinations, and incompatible build dependencies. Updating pip in the virtual environment can help with newer wheels, but cannot make an unsupported package compatible.
Can the file include other files or repositories?
Yes: -r base.txt includes another requirements file, supported index options configure package lookup, and VCS requirements can identify a repository. Not every pip command-line option is allowed in a requirements file. Keep credentials out of committed files and pin repository revisions where reproducibility matters.
References: Requirements file format, pip install, pip freeze, Repeatable pip installations.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan