Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

flake8 keeps reporting I001 even after the imports look sorted

Your error

I001 isort found an import in the wrong position2 more lines it can show

Fix it fast

Most common cause, 1 of 4

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

Fix
python -m pip show isort | grep ^Version
isort .
flake8 --select=I .
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to flake8-isort 7.1.0, isort 9.0.2, flake8 7.5.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1The imports really are out of order for isortThe printed diff moves imports between groups or reorders names, and isort --check-only fails on the same file
  2. 2black reformats the imports and isort has no black profileRight after black runs, I001 (often with I005) comes back on long from-imports that black joined or wrapped
  3. 3flake8-isort reads a different config than your isort commandisort . run by hand passes, yet flake8 still reports I001; the show-config output for that file lacks your profile or names another file
  4. 4pre-commit pins a different isort version than the one flake8 usesRunning 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 position
  • I003 isort expected 1 blank line in imports, found 0
  • I004 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.

Check
isort --check-only --diff app/models.py

Fix

Let isort rewrite the files, using the same isort that flake8 imports from your environment, then run flake8 again.

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

Verify
isort --check-only . && echo sorted

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Check
grep -n "profile" pyproject.toml setup.cfg .isort.cfg tox.ini 2>/dev/null

Fix

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.

Fix
# pyproject.toml
[tool.isort]
profile = "black"

Verify

Run black, then isort, then flake8. The second and third steps change and report nothing.

Verify
black . && isort --check-only . && flake8 --select=I .

Confirmed by isort.readthedocs.io

Did this fix it?

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.

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

Fix
# 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.

Verify
isort src/pkg/deep/module.py --show-config | grep -i pyproject

Confirmed by isort.readthedocs.io

Did this fix it?

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.

Check
grep -n -A2 "pycqa/isort" .pre-commit-config.yaml
python -m pip show isort | grep ^Version

Fix

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.

Fix
# .pre-commit-config.yaml
- repo: https://github.com/pycqa/isort
  rev: 9.0.2
  hooks:
    - id: isort

Verify

pre-commit and flake8 both pass on a clean checkout.

Verify
pre-commit run isort --all-files && flake8 --select=I .

Confirmed by raw.githubusercontent.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.