Sub topic 11
Sorting imports with Ruff
This chapter assumes you can run ruff check, read rule codes, and edit pyproject.toml. Import sorting in Ruff is a set of lint rules, not a separate tool. It uses the I prefix and follows isort's conventions.
How import sorting works
The I001 rule reports an import block that is unsorted or badly formatted. It is not part of Ruff's default rule set, so you have to enable the I prefix. Because I001 has a fix, ruff check --fix rewrites the block for you. ruff format does not sort imports.
Ruff groups imports into sections and sorts each section alphabetically. The default section order is:
__future__imports- Standard library
- Third-party packages
- First-party code (your own project)
- Local relative imports (
from . import x)
A blank line separates each section.
Enabling the rules
Add I to the selected rules. Keep the defaults by extending them rather than replacing them.
[tool.ruff.lint]
extend-select = ["I"]
[tool.ruff.lint.isort]
known-first-party = ["myapp"]
combine-as-imports = trueextend-select adds to the defaults, while select replaces them. Use whichever matches the rest of your configuration, but do not mix up the two.
A worked example
This file has imports in the wrong order and in the wrong sections:
import requests
import os
from myapp import utils
import sys
from . import helpers
from collections import defaultdictRun the check with a diff to preview the change:
ruff check --select I --diff example.pyApply it with:
ruff check --select I --fix example.pyThe result is:
import os
import sys
from collections import defaultdict
import requests
from myapp import utils
from . import helpersWithin a section, Ruff places plain import x statements before from x import y statements by default. This is the force-sort-within-sections = false behavior. Set it to true to sort both forms together by module name.
Common errors
Your package lands in the third-party section. Ruff detects first-party code by looking for the module under the project src setting, which defaults to the project root and src. If your layout differs, set src = ["lib"] under [tool.ruff], or list names in known-first-party.
The fix does nothing. I001 only reports a whole block that differs from the expected result. If the block contains a statement that is not an import, such as sys.path.append(...), Ruff treats the imports before and after it as separate blocks and sorts each one alone.
Sorting and formatting disagree. They do not conflict when you use ruff format and the I rules together. The order matters in a script, though. Run ruff check --fix first, then ruff format, because the formatter can change blank lines that the import fix also touches.
Unused imports are removed too. If you run ruff check --fix with F rules selected, F401 deletes unused imports as well. In an __init__.py this may remove re-exports you meant to keep. Use a per-file ignore for F401 there, or declare an __all__ list.
Summary
Enable I, tell Ruff what your first-party packages are, and let --fix do the sorting. Check that the sections come out as you expect on one file before you apply the fix to the whole project.
Exercise
Your project's package shop lives in lib/shop. After ruff check --select I --fix, import shop appears in the third-party section. Which two settings can fix this?
Show answer
Set src = ["lib"] under [tool.ruff] so Ruff finds the package in lib, or add known-first-party = ["shop"] under [tool.ruff.lint.isort].
Exercise
Why should you run ruff check --fix before ruff format in a script?
Show answer
The import fix and the formatter both change blank lines. Running the fix first lets the formatter normalize the final result, so a second run produces no further changes.