Requirements.txt
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:
Sample requirements.txt Contents:
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:
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:
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)
requirements-dev.txt (Local Development & CI/CD)
To configure a local developer environment:
5. Best Practices Checklist
- Commit
requirements.txtto Version Control: Always commitrequirements.txtto 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 freezeonly 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.
Project: Package-Ready Mini App
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Using pip for Package Management | Project: Package-Ready Mini App |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.