A flake8 plugin that helps you write tidier imports by enforcing rules on import aliases, banned modules, and relative imports. It is currently at version 4.12.0, actively maintained, and supports Python versions 3.10 and newer. Releases typically occur a few times a year, with major versions introducing significant changes.
pip install flake8-tidy-importsNo compatibility data collected yet for this library.
After installation, `flake8-tidy-imports` automatically registers itself with `flake8`. You typically configure it via a `setup.cfg`, `pyproject.toml`, or `.flake8` file. The primary options are `banned-modules` to specify disallowed imports with custom messages and `ban-relative-imports` to control relative import restrictions. If you use `flake8`'s `select` option, ensure you include `I25` to activate the plugin's checks.
If you explicitly selected or ignored `flake8-tidy-imports` rules in your `flake8` configuration (e.g., `select = I20`), you must update them to the new `I25` prefix (e.g., `select = I25`).
Ensure your project runs on Python 3.4+ (for older `flake8-tidy-imports` versions) or Python 3.10+ (for current 4.x versions).
Add `I25` (to enable all `flake8-tidy-imports` rules) or specific rules like `I250`, `I251`, `I252` to your `select` configuration in your `flake8` config file (e.g., `select = E,W,F,I25`).
Use patterns like `example.yellow.*` to match `example.yellow`, `example.yellow.truck`, etc. You can ban `decimal.Decimal` as well as `decimal`.
Add `{python2to3}` to your `banned-modules` list in `flake8` configuration, e.g., `banned-modules = mock = use unittest.mock.\n{python2to3}`.Remove the redundant alias: `import foo` or `from foo import bar` instead of `import foo as foo` or `from foo import bar as bar`.
Replace the banned import with the recommended alternative provided in the error message or as configured in your `flake8` settings (e.g., `unittest.mock` instead of `mock`). Adjust your `banned-modules` configuration if the ban is intentional.
Prefer absolute imports (e.g., `from mypackage.module import foo`) over relative imports from parent modules. Alternatively, if appropriate, use relative imports from sibling modules (e.g., `from . import foo`) if your configuration allows it, or adjust the `ban-relative-imports` setting in your `flake8` configuration.
Ensure `flake8-tidy-imports` is installed (`pip install flake8-tidy-imports`) and that your `flake8` version is compatible (>=3.8). If you use a `select` option in your `flake8` configuration, ensure `I25` (or `I20` for versions <4.0.0) is included. Check your `pyproject.toml` or `.flake8` for correct syntax for `banned-modules` under the `[flake8]` section.
If you are using `flake8-tidy-imports` version 4.0.0 or newer, update your `flake8` configuration to use the new rule codes (e.g., `I251` instead of `I201`). If you intend to use an older version, ensure your environment matches that requirement. Consider upgrading `flake8-tidy-imports` to the latest version for the most current rules and features.