flake8-requirements
flake8-requirements is a plugin for the flake8 static analysis tool that checks for missing or unused package requirements in Python projects. It helps maintain clean and accurate dependency lists by comparing imported modules against entries in requirements files (e.g., `requirements.txt`) or `pyproject.toml`. The current version is 2.3.0, and its release cadence is generally tied to bug fixes, new features, or compatibility updates for `flake8`.
Common errors
-
R001 Missing dependency: <package_name>
cause The Python module `<package_name>` is imported in your source code but is not listed in the requirements file `flake8-requirements` is checking.fixAdd `<package_name>` to your `requirements.txt` (or equivalent file) or ensure that the correct requirements file is being checked with `--requirements-file`. -
R002 Unused dependency: <package_name>
cause The package `<package_name>` is listed in your requirements file, but `flake8-requirements` could not detect any imports or uses of it in your source code.fixVerify if `<package_name>` is truly unused. If so, remove it from your requirements file. If it's used indirectly (e.g., via a framework, dev tool, or as a plugin), you may need to ignore this specific error or reconsider your dependency strategy. -
R004 Invalid requirements file format
cause There is a line in your requirements file that `flake8-requirements` cannot parse correctly according to `pip`'s requirements file format.fixExamine the requirements file (especially the line indicated by the error if available) and correct any syntax errors or non-standard entries. Refer to `pip`'s documentation on requirements file format. -
ERROR: flake8-requirements: The requirements file 'requirements.txt' does not exist.
cause `flake8-requirements` could not find the default `requirements.txt` file in the current directory or the file specified by the `--requirements-file` option.fixCreate a `requirements.txt` file in your project's root directory, or use the `--requirements-file path/to/your/reqs.txt` option to point to the correct file location and name.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes, including dropping Python 2 support (requiring Python 3.8+). The `--ignore-unregistered-requirements` option was renamed to `--always-assume-pip-installed`, and the R003 error code (multiple declarations) was removed.
- gotcha By default, `flake8-requirements` looks for `requirements.txt` in the current working directory. If your requirements file is named differently or located elsewhere (e.g., `requirements/prod.txt`, `pyproject.toml`), the plugin will not find it, leading to incorrect checks or an error about a missing file.
- gotcha The plugin may produce false positives for 'R002 Unused dependency' when packages are not directly imported but are used indirectly (e.g., via entry points, `setuptools` hooks, development tools, or for configuring external services).
- gotcha The parser for requirements files might struggle with complex or non-standard entries (e.g., VCS URLs like `git+https://...`, editable installs `file:.`, local paths, or specific environment markers) leading to `R004 Invalid requirements file format` or missed checks.
Install
-
pip install flake8-requirements
Quickstart
#!/bin/bash
# Create a dummy project for demonstration
mkdir my_project
cd my_project
echo "import requests\nprint('Hello')" > app.py
echo "requests" > requirements.txt
# Install flake8 and the plugin
pip install flake8 flake8-requirements
# Run flake8 with the plugin
flake8 .
# Example of a missing dependency error (R001)
echo "import unknown_lib\nimport requests" > app.py
flake8 .
# Example of an unused dependency error (R002)
echo "requests\nunused_lib" > requirements.txt
echo "import requests" > app.py
flake8 .