Registry / testing / flake8-requirements

flake8-requirements

JSON →
library2.3.0pypypiunverified

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`.

pip install flake8-requirements
INSTALL
IMPORT
SIG · FLAKE8-REQUIREMENT
F
flake8-requirements
testingpythonv2.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

After installing `flake8-requirements`, it automatically integrates with `flake8`. Simply run `flake8` in your project directory. The plugin will check your imports against your `requirements.txt` (or a file specified by `--requirements-file`) and report missing (R001) or unused (R002) dependencies.

#!/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 .
flake8 --version
Debug
Known issues
breakingVersion 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.
fix
Ensure your project runs on Python 3.8 or newer. Update your flake8 configuration to use `--always-assume-pip-installed` if you previously used the old flag. Review your dependencies for any R003 issues, which must now be handled manually.
affects: >=2.0.0
gotchaBy 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.
fix
Always specify the correct path to your requirements file using the `--requirements-file` option (e.g., `flake8 --requirements-file requirements/prod.txt`) or ensure your `pyproject.toml` is configured correctly if using that format.
affects: All versions
gotchaThe 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).
fix
For packages that are legitimately required but trigger R002, consider adding them to a development-specific requirements file that is not checked, or ignore the specific R002 error for those packages if you're certain they are needed.
affects: All versions
gotchaThe 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.
fix
Simplify complex requirements entries where possible. For advanced cases, you might need to manually verify the dependencies or use the `--always-assume-pip-installed` option carefully. Check the project's GitHub issues for known parsing limitations.
affects: All versions
Errors
Common errors & fixes
R001 Missing dependency: <package_name>
The Python module `<package_name>` is imported in your source code but is not listed in the requirements file `flake8-requirements` is checking.
fix
Add `<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>
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.
fix
Verify 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
There is a line in your requirements file that `flake8-requirements` cannot parse correctly according to `pip`'s requirements file format.
fix
Examine 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.
`flake8-requirements` could not find the default `requirements.txt` file in the current directory or the file specified by the `--requirements-file` option.
fix
Create 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.
Upgrade
Version history
2.3.0latest on PyPI · released Aug 23, 2025
Audit
Dependencies
flake8requiredThis library is a plugin for flake8 and requires it to run.
Agent activity
2 hits · last 30 days
node
2
Resources