Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Requirements Processor

Purpose

Generates a requirements.txt file for a Python project by scanning the project’s .py source files for import statements and listing the third-party PyPI distributions they reference.

How It Works

  1. Scans every .py file in the project’s source directories.
  2. Extracts the top-level module name from each import / from statement.
  3. Drops imports that resolve to a local project file (intra-project imports).
  4. Drops imports that are part of the Python standard library.
  5. Drops imports listed in exclude.
  6. Maps each remaining import name to its PyPI distribution name using the built-in curated table (e.g. cv2opencv-python, yamlPyYAML). User-supplied mapping entries win over the built-in table.
  7. Writes the deduplicated result to requirements.txt.

Import → Distribution Mapping

Most Python packages publish under the same name as their top-level import, so the default is identity (import requestsrequests). A curated table handles the common exceptions:

ImportDistribution
cv2opencv-python
yamlPyYAML
PILPillow
sklearnscikit-learn
bs4beautifulsoup4
dateutilpython-dateutil
dotenvpython-dotenv
jwtPyJWT

Projects that import an unusual name should add an override:

[processor.requirements.mapping]
internal_tools = "acme-internal-tools"

Limitations

  • No version pinning. The generated file lists bare distribution names. Running pip freeze > requirements.txt is the right tool if you need pinned versions.
  • Static analysis only. Conditional imports inside try blocks, runtime __import__ calls, and string-based imports are not detected.
  • Curated mapping is finite. Packages with import/distribution name mismatches not in the built-in table default to identity; add them to mapping when needed.

Source Files

  • Input: **/*.py (configurable via src_dirs / src_extensions)
  • Output: requirements.txt (configurable via output)

Configuration

[processor.requirements]
output = "requirements.txt"    # Output file path
exclude = []                   # Import names to never emit
sorted = true                  # Sort entries alphabetically
header = true                  # Include a "# Generated by rsconstruct" header

[processor.requirements.mapping]
# Per-project overrides: import_name = "pypi-distribution-name"
# These win over the built-in curated table.
KeyTypeDefaultDescription
outputstring"requirements.txt"Output file path
excludestring[][]Import names to never emit
sortedbooltrueSort entries alphabetically (false preserves first-seen order)
headerbooltrueInclude a comment header line
mappingmap{}Per-project import→distribution overrides

Batch support

Runs as a single whole-project operation — all .py files feed into one requirements.txt output.