Python
pip
requirements.txt
comments
programming

How to add comments in a pip requirements.txt file

Codemia Knowledge Hub ·

Yes. pip ignores any line that starts with #, and a # preceded by whitespace starts an inline comment that runs to the end of the line (a # inside a URL fragment of a direct reference is not a comment). Comments are a good way to explain why a package is pinned, which dependency is temporary, or what platform constraint applies.

Comment syntax in requirements.txt

text
1# Core runtime dependencies
2fastapi==0.116.0
3uvicorn==0.35.0
4
5# Temporary pin due to upstream API change
6httpx==0.28.1
7
8# Testing
9pytest==8.4.1  # required for unit and integration tests

This works with pip install -r requirements.txt and keeps the intent visible to future maintainers.

Splitting large dependency sets with -r

For bigger projects, split dependencies into layered files and comment each one's purpose.

text
1# requirements.txt
2-r requirements/base.txt
3-r requirements/dev.txt
4
5# requirements/base.txt
6Django==5.1.4
7psycopg[binary]==3.2.9
8
9# requirements/dev.txt
10-r base.txt
11ipython==9.4.0
12mypy==1.16.1

This avoids one giant file and keeps production installs cleaner.

Comments in constraints files

Constraints files do not install packages; they only restrict versions. Comments help there too.

text
1# constraints.txt
2# pinned for compatibility with internal plugin
3urllib3==2.5.0
4
5# security baseline
6cryptography==45.0.5

Install with:

bash
pip install -r requirements.txt -c constraints.txt

Recording why a constraint exists saves time during upgrades and incident response.

Comments next to environment markers

Environment markers combine well with inline comments:

text
uvloop==0.21.0 ; platform_system == "Linux"  # Linux event loop acceleration
pywin32==311 ; platform_system == "Windows"  # Windows-only APIs

Markers keep one dependency spec working across several operating systems.

Where comments belong with pip-tools

With pip-tools, keep human-written comments in the input file and generate the pinned output.

bash
pip-compile requirements.in --output-file requirements.txt
pip-sync requirements.txt

Here comments belong in requirements.in, and requirements.txt is treated as generated output.

Keeping comments useful in a team repo

  • Group dependencies by purpose and keep comments short. Put longer rationale on its own line above the package.
  • Update versions and their comments in the same change, so notes do not go stale. Revisit nearby comments during security upgrades too, since notes about temporary pins or compatibility limits may no longer be true after a patch.
  • For a temporary package, write down the condition for removing it.

Mistakes that break installs or mislead

  • Explanatory text on its own line without # is parsed as a requirement and the install fails.
  • Comments in tool-generated files are often overwritten on regeneration.
  • Inline comments become misleading when the version changes and the comment does not.
  • Do not put secrets or internal URLs in comments. Requirements files are usually committed to source control.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms