Requirements.txt0%

Requirements.txt

Beginner12 min readUpdated: Jul 11, 2026
Study Materials

Working with requirements.txt in Python

In professional software development, sharing code requires sharing its exact dependencies. Instead of telling teammates or DevOps engineers to manually install multiple individual libraries, Python projects use a standardized manifest file called requirements.txt. This file lists all third-party libraries and version constraints needed to run the application reliably.


1. Exporting Dependencies with pip freeze

When your virtual environment is active and you have installed all necessary packages, export the complete list of installed packages and their exact versions using pip freeze:

Bash / Terminal
# Exports active environment packages to requirements.txt
python -m pip freeze > requirements.txt

Sample requirements.txt Contents:

Output
# Production dependencies
requests==2.31.0
certifi==2024.7.4
urllib3==2.2.2
charset-normalizer==3.3.2
idna==3.7

Each line consists of a package name followed by comparison operators and version numbers.


2. Installing from requirements.txt

When cloning a project repository on a new machine or deploying to a cloud server (e.g. AWS, Docker, Heroku), initialize a fresh virtual environment and install all dependencies in a single step using the -r flag:

Bash / Terminal
# 1. Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\Activate.ps1
 
# 2. Install all dependencies from the file
python -m pip install -r requirements.txt

pip reads every line, resolves transitive dependencies, and installs the exact versions specified.


3. Version Specifiers and Syntax Rules

requirements.txt supports flexible version constraint rules defined by PEP 440:

Output
# 1. Exact match (Deterministic / Immutable build)
requests == 2.31.0
 
# 2. Minimum version
pandas >= 2.0.0
 
# 3. Version range
django >= 4.2, < 5.0
 
# 4. Compatible release (Allows bugfixes up to next minor version)
# Equivalent to: >= 1.2.0, == 1.2.*
fastapi ~= 0.110.0
 
# 5. Direct installation from a Git repository
git+https://github.com/psf/requests.git@v2.31.0

4. Multi-Environment Architecture: Dev vs. Prod

Production containers should not be bloated with testing tools, debuggers, or linters. The standard industry pattern splits requirements into separate files:

requirements.txt (Core Production)

Output
requests==2.31.0
flask==3.0.3
gunicorn==22.0.0

requirements-dev.txt (Local Development & CI/CD)

Output
# Include all production requirements first:
-r requirements.txt
 
# Additional developer tools:
pytest==8.2.0
black==24.4.2
flake8==7.0.0
mypy==1.10.0

To configure a local developer environment:

Bash / Terminal
python -m pip install -r requirements-dev.txt

5. Best Practices Checklist

  • Commit requirements.txt to Version Control: Always commit requirements.txt to Git so your entire team uses matching dependencies.
  • Pin Exact Versions in Production: Use == for production builds to prevent unexpected breaking changes when libraries publish new releases.
  • Avoid Stale Global Freezes: Run pip freeze only inside a clean, project-specific virtual environment—never in your global Python environment, which contains unrelated system packages.

Multiple Choice Questions

1. Which command generates a requirements.txt file listing all packages installed in the active environment?

A. pip export requirements.txt B. pip save > requirements.txt C. pip freeze > requirements.txt D. pip dump requirements.txt Answer: C Explanation: pip freeze outputs installed packages and exact versions in requirement format, which can be redirected (>) into requirements.txt.


2. How do you install all packages listed in a requirements.txt file?

A. pip install requirements.txt B. pip install -r requirements.txt C. pip load requirements.txt D. pip read requirements.txt Answer: B Explanation: The -r (or --requirement) flag instructs pip to read and install dependencies from the specified file.


3. How does requirements-dev.txt typically reference the base requirements.txt file?

A. import requirements.txt B. include requirements.txt C. -r requirements.txt D. @requirements.txt Answer: C Explanation: The -r requirements.txt directive inside a requirements file tells pip to include and parse the referenced requirements file recursively.


4. What does the version constraint django >= 4.0, < 5.0 specify?

A. Any version of Django greater than or equal to 4.0, but strictly less than 5.0 B. Any version of Django except 4.5 C. Django 4.0 and Django 5.0 simultaneously D. Only Django 4.0 Answer: A Explanation: The comma represents a logical AND, restricting the permissible version to 4.x releases without upgrading to breaking 5.x releases.


5. Why is running pip freeze inside a global Python environment discouraged?

A. It causes Python to crash B. It captures unrelated packages from other projects and system tools, resulting in a bloated, incorrect manifest C. It deletes installed packages D. It cannot write to text files Answer: B Explanation: Running pip freeze in the global environment captures every package ever installed on your system. It should always be executed within an isolated project virtual environment.


Next Lesson

Project: Package-Ready Mini App

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext