Handling Comments and -r Includes

Learn how comments (#) and -r include directives in requirements.txt are handled during conversion to pyproject.toml format.

Getting Started

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:

  1. Comments (#) -- Preserved as-is in the output. Both formats use the same comment syntax.
  2. -r filename -- Cannot be represented in pyproject.toml. Converted to a comment noting the skipped directive. You need to manually merge the referenced file's contents.
  3. --index-url and other pip options -- These are pip-specific flags, not dependency specifications. They're converted to comments. In pyproject.toml, custom indexes can be configured in [tool.pip] or through pip.conf.
  4. 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

Open full tool