A flake8 plugin that helps you write tidier imports.
Linting a Django project? Check out my book Boost Your Django DX which covers Flake8 and many other code quality tools.
Requirements
Python 3.9 to 3.14 supported.
Installation
First, install with pip:
python -m pip install flake8-tidy-imports
Second, if you define Flake8’s select setting, add the I25 prefix to it. Otherwise, the plugin should be active by default.
Options
banned-modules
Config for rule I251 (below). Should contain a map where each line is a banned import string, followed by ‘=’, then the message to use when encountering that import.
There is also a special directive to ban a preselected list of removed/moved modules between Python 2 and Python 3, recommending replacements from six where possible. It can be turned on by adding {python2to3} to the list of banned-modules.
For example in setup.cfg:
[flake8]
banned-modules =
mock = Use unittest.mock.
{python2to3}
Note that despite the name, you can ban imported objects too, since the syntax is the same. For example:
[flake8]
banned-modules =
decimal.Decimal = Use ints and floats only.
Entries containing * are treated as wildcards matching zero or more path components. For example:
example.yellow.* matches example.yellow, example.yellow.truck, example.yellow.truck.driving etc.
example.*.truck matches example.truck, example.yellow.truck, example.red.truck, example.big.red.truck, etc.
ban-relative-imports
Controls rule I252 (below). Accepts two values:
parents - bans imports from parent modules (and grandparents, etc.), i.e. with more than one ..
true - bans all relative imports.
For example:
[flake8]
ban-relative-imports = parents
(If you want to ban absolute imports, you can put your project’s modules in banned-modules.)
Rules
Note: Before version 4.0.0, the rule codes were numbered 50 lower, e.g. I250 was I200. They were changed in Issue #106 due to conflict with flake8-import-order.
I250: Unnecessary import alias
Complains about unnecessary import aliasing of three forms:
import foo as foo -> import foo
import foo.bar as bar -> from foo import bar
from foo import bar as bar -> from foo import bar
The message includes the suggested rewrite (which may not always be correct), for example:
$ flake8 file.py
file.py:1:1: I250 Unnecessary import alias - rewrite as 'from foo import bar'.
Such aliases can be automatically fixed by isort if you activate its remove_redundant_aliases option.
I251: Banned import <import> used.
Complains about use of banned imports. By default there are no imports banned - you should configure them with banned-modules as described above in ‘Options’.
The message includes a user-defined part that comes from the configuration. For example:
$ flake8 file.py
file.py:1:1: I251 Banned import 'mock' used - use unittest.mock instead.
I252: Relative imports <from parent modules> are banned.
Complains about use of relative imports:
from . import foo (sibling import)
from .bar import foo (sibling import)
from .. import foo (parent import)
Controlled by the ban-relative-imports configuration option.
Absolute imports, or relative imports from siblings, are recommended by PEP8:
Absolute imports are recommended, as they are usually more readable and tend to be better behaved…
import mypkg.sibling from mypkg import sibling from mypkg.sibling import exampleHowever, explicit relative imports are an acceptable alternative to absolute imports…
from . import sibling from .sibling import example
See also
For more advanced control of imports in your project, try import-linter.
Metadata
Release files for flake8-tidy-imports 4.12.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flake8_tidy_imports-4.12.0.tar.gz | 13.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flake8_tidy_imports-4.12.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.0 kB
Release files / flake8_tidy_imports-4.12.0.tar.gz
| Download URL | flake8_tidy_imports-4.12.0.tar.gz |
|---|---|
| Size | 13.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9254788c3b6862c2fcec0250d2dc9af089afebff9c5b8a8ac8b9525b059b06db
|
|
BLAKE2b-256 checksum How to use checksums |
4d24ce374a8028f3f3d2c48082693d5b4a2b7073e6be64579ecff20bcefdb812
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2025.
Transparency logRelease files / flake8_tidy_imports-4.12.0-py3-none-any.whl
| Download URL | flake8_tidy_imports-4.12.0-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ab1e31a5ce07518a31c0a34cd92551f4c27639ae2c35a21364680a0318da312e
|
|
BLAKE2b-256 checksum How to use checksums |
9316bc792adda2b19d501f474bbf6eb0b35546db01f4816459153bd69a388d03
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 8, 2025.
Transparency log