Module 14 · Python Modules and Packages — topic 12 of 13
requirements.txt
Recording project dependencies: the requirements.txt format, pinning versions, comments, generating the file with pip freeze, installing from it, and a brief look at pyproject.toml.
Learning objectives
- Write and read a requirements.txt file
- Generate one with pip freeze
- Decide between pinned and flexible versions
Definition
requirements.txt is a plain-text file listing the packages a project needs, one requirement per line, optionally with version specifiers. pip install -r requirements.txt installs them all.
Introduction
A new teammate clones your project. One command — pip install -r requirements.txt — should give them exactly the packages they need.
Why it matters
Recording dependencies makes projects reproducible on other machines, servers and CI systems.
Detailed explanation
Each line is a requirement such as requests==2.32.3 or flask>=3.0,<4. Lines starting with # are comments; trailing # comments are allowed too.
Applications usually pin exact versions (==) so every install is identical. Libraries specify ranges so they work alongside other packages.
pip freeze > requirements.txt writes every package in the active environment with its exact version, including indirect dependencies.
A common approach keeps a short hand-written list of direct dependencies and a generated 'lock' file with everything pinned.
pip install -r requirements.txt installs everything listed; -r other.txt inside a file includes another file, useful for separate development requirements.
Modern projects also describe dependencies in pyproject.toml under [project] dependencies, used when building and installing the project as a package.
Update dependencies deliberately: change versions, reinstall, run the tests, then commit the updated file.
Never put secrets such as access tokens in requirements files, because they are committed to version control.
Syntax
- One requirement per line.
- Pin versions for applications.
- Comments start with #.
Code examples
A requirements file
Generating and installing
Run freeze inside the project's activated virtual environment.
More examples
Parsing a requirements file
['requests==2.32.3', 'flask>=3.0,<4', 'pandas==2.2.3', 'numpy'] 2 of 4 pinned
Step-by-step explanation
- Comments are removed by splitting on # and keeping the first part.
- Blank lines are skipped.
- Counting pinned requirements shows which dependencies could change between installs.
Expected output
Running “Parsing a requirements file” prints exactly:
Practical applications
- Deploying web apps to servers that run pip install -r requirements.txt.
- CI pipelines installing dependencies before running tests.
Best practices
- Keep requirements.txt in version control.
- Pin versions for applications.
- Generate it from a clean virtual environment.
Common mistakes
Unpinned application dependencies
Avoid
Prefer
A new release could break the app on the next install.
Important notes
- requirements-dev.txt often starts with -r requirements.txt and adds testing tools.
Key takeaways
- requirements.txt lists dependencies line by line.
- pip freeze creates it; pip install -r uses it.
- Pin versions for reproducible installs.
Try it yourself
- 1Write a requirements.txt for a project using requests and rich, with sensible version rules.
- 2Extend the parser to list unpinned packages separately.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions cover the “Python Modules and Packages” module. Answers and explanations are revealed after you submit.
- ModerateWhat happens the second time a module is imported in the same program?
- ModerateCode under if __name__ == "__main__": runs when…
- ModerateWhat is printed?