Handling Comments and -r Includes
Learn how comments (#) and -r include directives in requirements.txt are handled during conversion to pyproject.toml format.
Detailed Explanation
The requirements.txt format supports comments (lines starting with #) and include directives (-r to include another requirements file). Both need special handling during conversion.
requirements.txt with comments and includes:
# Core web framework
flask>=3.0
flask-cors>=4.0
# Include shared base requirements
-r requirements-base.txt
# Database
sqlalchemy>=2.0
# These are pinned for security reasons
cryptography==41.0.7 # CVE-2023-49083 fix
# Optional features
-r requirements-optional.txt
--index-url https://pypi.org/simple
Converted to pyproject.toml:
[project]
name = ""
version = "0.1.0"
requires-python = ">=3.8"
# Core web framework
# Include shared base requirements
# Skipped: -r requirements-base.txt
# These are pinned for security reasons
# Skipped: -r requirements-optional.txt
# Skipped: --index-url https://pypi.org/simple
dependencies = [
"flask>=3.0",
"flask-cors>=4.0",
"sqlalchemy>=2.0",
"cryptography==41.0.7",
]
How each element is handled:
- Comments (
#) -- Preserved as-is in the output. Both formats use the same comment syntax. -r filename-- Cannot be represented inpyproject.toml. Converted to a comment noting the skipped directive. You need to manually merge the referenced file's contents.--index-urland other pip options -- These are pip-specific flags, not dependency specifications. They're converted to comments. Inpyproject.toml, custom indexes can be configured in[tool.pip]or throughpip.conf.- Inline comments -- The text after a dependency (like
# CVE-2023-49083 fix) is stripped during parsing. TOML supports inline comments with#, but they go on a separate line.
To handle -r includes properly:
Before converting, manually concatenate all included files into a single list. For example, combine requirements-base.txt and requirements.txt into one file, then convert.
Use Case
Converting a requirements.txt that uses -r includes to reference a shared base requirements file across multiple projects in a monorepo.
Try It — Requirements ↔ Pyproject
Related Topics
Basic requirements.txt to pyproject.toml
Getting Started
Optional Dependency Groups in pyproject.toml
Advanced Features
Development Dependencies and Optional Groups
Advanced Features
Converting pyproject.toml Back to requirements.txt
Reverse Conversion
Converting Pinned Version Dependencies
Version Specifiers