Registry / testing / flake8-tidy-imports

flake8-tidy-imports

JSON →
library4.12.0pypypiunverified

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-imports
INSTALL
IMPORT
SIG · FLAKE8-TIDY-IMPORT
F
flake8-tidy-imports
testingpythonv4.12.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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.

import os # Example file: my_module.py # import json as json # I250: Unnecessary import alias # from datetime import timedelta as timedelta # I250: Unnecessary import alias # from ..utils import helper # I252: Relative import from parent module (if configured) # import mock # I251: Banned import 'mock' used - use unittest.mock instead. (if configured) # Configure flake8 in setup.cfg or pyproject.toml # [flake8] # banned-modules = # mock = use unittest.mock instead. # django.utils.six = no more six! # {python2to3} # ban-relative-imports = parents # select = I25 # To run flake8: # flake8 my_module.py # For demonstration purposes, this code is not meant to be run directly # but shows common violations flake8-tidy-imports would catch. # The actual plugin operates via the flake8 command-line tool. print("Run 'flake8 <your_file.py>' after configuring flake8-tidy-imports.")
Debug
Known issues
breakingRule codes were renumbered in version 4.0.0. All rule codes were increased by 50 (e.g., I200 became I250).
fix
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`).
affects: <4.0.0 to 4.0.0+
breakingPython 2 support was dropped in version 2.0.0.
fix
Ensure your project runs on Python 3.4+ (for older `flake8-tidy-imports` versions) or Python 3.10+ (for current 4.x versions).
affects: <2.0.0 to 2.0.0+
gotchaIf `flake8`'s `select` option is defined, `flake8-tidy-imports` rules (I25x) must be explicitly added for the plugin to be active.
fix
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`).
affects: All versions
gotchaThe `banned-modules` configuration can use wildcards (`*`) to match zero or more path components. It also allows banning imported objects, not just modules.
fix
Use patterns like `example.yellow.*` to match `example.yellow`, `example.yellow.truck`, etc. You can ban `decimal.Decimal` as well as `decimal`.
affects: All versions
gotchaA special `{python2to3}` directive can be used within `banned-modules` to automatically ban a preselected list of Python 2-era modules and recommend their Python 3/six replacements.
fix
Add `{python2to3}` to your `banned-modules` list in `flake8` configuration, e.g., `banned-modules = mock = use unittest.mock.\n{python2to3}`.
affects: All versions
Errors
Common errors & fixes
I250 Unnecessary import alias - rewrite as 'from foo import bar'
This error occurs when an import is aliased unnecessarily, such as `import foo as foo`, which the plugin considers redundant.
fix
Remove the redundant alias: `import foo` or `from foo import bar` instead of `import foo as foo` or `from foo import bar as bar`.
I251 Banned import 'mock' used - use unittest.mock instead.
This error indicates that you are using a module or object that has been explicitly banned in your `flake8` configuration under the `banned-modules` option. The message often includes a user-defined suggestion for a replacement.
fix
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.
I252 Relative imports from parent modules are banned.
This error is triggered when your code uses relative imports that traverse up the package hierarchy (e.g., `from .. import foo`), and the `ban-relative-imports` option in your `flake8` configuration is set to ban such imports.
fix
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.
Option "banned-modules" is not registered. Ignoring.
This message, often seen in `flake8` debug output, indicates that `flake8` is not recognizing the `banned-modules` option. This typically happens if `flake8-tidy-imports` is not correctly installed or enabled, or if there's a conflict with other plugins or an older `flake8` version.
fix
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.
I201 Banned import 'foo' used
This is an older error code for banned imports. Before version 4.0.0, `flake8-tidy-imports` rules were numbered 50 lower (e.g., I200, I201, I202). This error indicates you might be running an older version of the plugin or your configuration still references the old codes after an upgrade.
fix
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.
Upgrade
Version history
4.12.0latest on PyPI · released Sep 8, 2025
Audit
Dependencies
flake8requiredflake8-tidy-imports is a plugin for Flake8 and requires it to run.
Agent activity
18 hits · last 30 days
node
18
Resources
flake8-tidy-imports — pip install flake8-tidy-imports · libregistry