flake8 keeps reporting I001 even after the imports look sorted
Fix it fast
Most common cause, 1 of 4The imports really are out of order for isort
Let isort rewrite the files, using the same isort that flake8 imports from your environment, then run flake8 again.
python -m pip show isort | grep ^Version
isort .
flake8 --select=I .Short answer
flake8 error I001 isort found an import in the wrong position comes from flake8-isort, which asks isort to re-sort the file and reports every line isort would move. Usual causes: the imports are unsorted, black and isort disagree without profile black, or flake8 finds a different config than your isort run. Applies to flake8-isort 7.1.0 with isort 9.0.2.
Which cause do you have?
Ask flake8-isort to print the diff it got from isort for one failing file, then ask isort which config file it used for that same path. The diff shows what would move; the config output shows whether a profile and your settings were picked up at all.
flake8 --select=I --isort-show-traceback app/models.py
isort app/models.py --show-config | grep -E '"(profile|source)"'Then find the line you see in this list and open its fix:
- 1The imports really are out of order for isort
The printed diff moves imports between groups or reorders names, and isort --check-only fails on the same file - 2black reformats the imports and isort has no black profile
Right after black runs, I001 (often with I005) comes back on long from-imports that black joined or wrapped - 3flake8-isort reads a different config than your isort command
isort . run by hand passes, yet flake8 still reports I001; the show-config output for that file lacks your profile or names another file - 4pre-commit pins a different isort version than the one flake8 uses
Running isort through pre-commit and through flake8 on the same commit gives opposite results
What does the error look like?
I001 isort found an import in the wrong positionI003 isort expected 1 blank line in imports, found 0I004 isort found an unexpected blank line in imports
Cause 1 of 4
The imports really are out of order for isort
You seeThe printed diff moves imports between groups or reorders names, and isort --check-only fails on the same file
Check
I001 is not a separate rule set. flake8-isort hands the file text to isort, compares the output with the original, and turns each changed line of that diff into I001, I003, I004 or I005. If the isort CLI with the same config also wants changes, the file simply needs sorting.
isort --check-only --diff app/models.pyFix
Let isort rewrite the files, using the same isort that flake8 imports from your environment, then run flake8 again.
python -m pip show isort | grep ^Version
isort .
flake8 --select=I .Verify
isort --check-only returns exit code 0 and flake8 prints no I0xx lines.
isort --check-only . && echo sortedConfirmed by raw.githubusercontent.com
Cause 2 of 4
black reformats the imports and isort has no black profile
You seeRight after black runs, I001 (often with I005) comes back on long from-imports that black joined or wrapped
Check
isort and black only agree on how to wrap and space long imports when isort's profile is set to black. Without it, each tool keeps undoing the other's layout, and flake8-isort reports the difference on every run.
grep -n "profile" pyproject.toml setup.cfg .isort.cfg tox.ini 2>/dev/nullFix
Set the profile in a config file at the repository root, so isort, pre-commit, editors and flake8-isort all read it. If you changed black's line length, set isort's line_length to the same number.
# pyproject.toml
[tool.isort]
profile = "black"Verify
Run black, then isort, then flake8. The second and third steps change and report nothing.
black . && isort --check-only . && flake8 --select=I .Confirmed by isort.readthedocs.io
Cause 3 of 4
flake8-isort reads a different config than your isort command
You seeisort . run by hand passes, yet flake8 still reports I001; the show-config output for that file lacks your profile or names another file
Check
flake8-isort builds isort's settings from the linted file's own folder, and isort climbs up from there to the closest config file, stops at the git root and never merges two files. Options given only on the command line, such as --profile black in a pre-commit hook or --settings-path, never reach flake8-isort.
isort app/models.py --show-config | grep '"source"'Fix
Move every option you pass on the command line into one config file at the project root, delete stray .isort.cfg or [isort] sections in subfolders, and drop the extra CLI arguments.
# pyproject.toml at the repository root
[tool.isort]
profile = "black"
src_paths = ["src", "tests"]Verify
The show-config output for a file deep in the tree now points at the root pyproject.toml, and flake8 and isort agree.
isort src/pkg/deep/module.py --show-config | grep -i pyprojectConfirmed by isort.readthedocs.io
Cause 4 of 4
pre-commit pins a different isort version than the one flake8 uses
You seeRunning isort through pre-commit and through flake8 on the same commit gives opposite results
Check
isort's output changes between major versions. The 6.0.0 notes turn on the magic trailing comma in the black profile, and 9.0.0 starts sorting lazy imports and treats private stdlib modules as stdlib. A hook pinned to an old rev can sort one way while the isort that flake8-isort imports sorts another.
grep -n -A2 "pycqa/isort" .pre-commit-config.yaml
python -m pip show isort | grep ^VersionFix
Use one isort version everywhere: set the pre-commit rev to the version in your dev requirements, or the other way round, then re-sort once.
# .pre-commit-config.yaml
- repo: https://github.com/pycqa/isort
rev: 9.0.2
hooks:
- id: isortVerify
pre-commit and flake8 both pass on a clean checkout.
pre-commit run isort --all-files && flake8 --select=I .Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Keep every isort option in one root config file and none on the command line.
- Pin isort to the same version in requirements-dev.txt and in the pre-commit rev, and bump both in one commit.
- Run isort before flake8 in CI so the lint step only fails on files someone forgot to sort.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.