flake8-isort
flake8-isort is a flake8 plugin that integrates the isort library to check the order of imports in Python files. It leverages isort's sorting logic and configuration (e.g., via .isort.cfg or pyproject.toml) to report import style violations as flake8 errors. Regularly maintained, its current version is 7.0.0, released in October 2025.
Warnings
- breaking Version 7.0.0 dropped support for Python 3.9 and requires `isort` 7.0.0. Ensure your Python and `isort` versions are compatible before upgrading.
- breaking Version 6.0.0 dropped support for `isort` 4.x and added support for `flake8` 6.0.0. Mismatched `flake8` or `isort` versions can lead to `FailedToLoadPlugin` errors.
- gotcha `flake8-isort` requires an `isort` configuration file (e.g., `.isort.cfg` or a `[tool.isort]` section in `pyproject.toml`) to function correctly. Without it, it will report an `I002` error ("no configuration found").
- gotcha Do not use `flake8-isort` simultaneously with `flake8-import-order`. These plugins offer similar functionalities for checking import order and can cause conflicts or redundant checks. `flake8-isort` defers all logic to `isort`.
- gotcha If using `flake8`'s `select` option (to only run specific checks), ensure the 'I' error code category is included (e.g., `--select=E,W,F,I`) to see `flake8-isort`'s reported issues (I001-I005).
Install
-
pip install flake8-isort flake8 isort -
conda install -c conda-forge flake8-isort
Quickstart
# 1. Install dependencies (if not already installed)
# pip install flake8-isort flake8 isort
# 2. Create an example Python file (e.g., `my_module.py`)
# This file has unsorted imports
# print("Content of my_module.py:")
# print("""
# import os
# import sys
# from datetime import datetime
# from collections import defaultdict
# """)
# 3. Create an isort configuration file (e.g., .isort.cfg or pyproject.toml)
# Example .isort.cfg:
# [isort]
# profile = black
# known_third_party = datetime,collections
#
# Or in pyproject.toml:
# [tool.isort]
# profile = "black"
# known_third_party = ["datetime", "collections"]
# 4. Run flake8 (flake8-isort will automatically integrate)
# For demonstration, let's create a dummy file and config
import os
# Create a dummy Python file
with open('temp_module.py', 'w') as f:
f.write('import os\nfrom sys import argv\nimport collections\n\ndef func(): pass\n')
# Create a dummy isort config file
with open('.isort.cfg', 'w') as f:
f.write('[isort]\nprofile = black\n')
# To run flake8 with flake8-isort, you'd typically execute from your shell:
# flake8 temp_module.py
# The output would show I001 (isort found an import in the wrong position)
# Clean up dummy files
os.remove('temp_module.py')
os.remove('.isort.cfg')