Skip to content

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.

Beginner 8 min read·Lesson 220 of 306 in Python Programming

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

Text
# requirements.txt
requests==2.32.3
flask>=3.0,<4
pandas==2.2.3 # pinned for reproducibility
# install everything
# python -m pip install -r requirements.txt
  • One requirement per line.
  • Pin versions for applications.
  • Comments start with #.

Code examples

A requirements file

Text
# web
requests==2.32.3
flask>=3.0,<4
# data
pandas==2.2.3
numpy

Generating and installing

Terminal
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt

Run freeze inside the project's activated virtual environment.

More examples

Parsing a requirements file

Python
from pathlib import Path
Path("requirements.txt").write_text('''# web
requests==2.32.3
flask>=3.0,<4
# data
pandas==2.2.3 # pinned for reproducibility
numpy
''', encoding="utf-8")
def parse(path):
reqs = []
for raw in Path(path).read_text(encoding="utf-8").splitlines():
line = raw.split("#", 1)[0].strip()
if line:
reqs.append(line)
return reqs
reqs = parse("requirements.txt")
print(reqs)
pinned = [r for r in reqs if "==" in r]
print(f"{len(pinned)} of {len(reqs)} pinned")
Output
['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:

TextOutput
['requests==2.32.3', 'flask>=3.0,<4', 'pandas==2.2.3', 'numpy']
2 of 4 pinned

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

requests
flask

Prefer

requests==2.32.3
flask==3.1.0

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

  1. 1
    Write a requirements.txt for a project using requests and rich, with sensible version rules.
  2. 2
    Extend 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?
Practise this module