How to sort Python imports with Ruff
Ruff sorts imports through its linter, not its formatter. Select the I rule and apply fixes to sort the imports in a file or directory:
$ uvx ruff check --select I --fix .
To make import sorting a permanent part of a project, add the I rule under [tool.ruff.lint] in pyproject.toml:
[tool.ruff.lint]
extend-select = ["I"]ruff check --fix . then sorts imports alongside every other enabled rule. The rest of this guide covers each path in detail.
Sort a single file or directory
To sort the imports in one file, pass its name:
$ uvx ruff check --select I --fix example.py
To sort every Python file in the current directory and its subfolders, pass .:
$ uvx ruff check --select I --fix .
--select I limits the run to import sorting and leaves every other lint rule off, which makes it a direct replacement for an isort invocation. The I prefix covers I001 (unsorted-imports), which reorders and groups imports, and I002 (missing-required-import), which adds any imports the project requires in every file. Drop --select I to run import sorting together with the rest of Ruff’s rules.
Why ruff format doesn’t sort imports
ruff format and ruff check do different jobs, and only ruff check sorts imports. The formatter rewrites code layout (indentation and line wrapping) to match a consistent style. Import order is a lint rule, I001, so it belongs to the linter.
Running the formatter on unsorted imports leaves them unsorted:
$ cat example.py
import pandas
import numpy as np
$ uvx ruff format example.py
1 file left unchanged
$ cat example.py
import pandas
import numpy as np
Running the linter with the I rule sorts them:
$ uvx ruff check --select I --fix example.py
Found 1 error (1 fixed, 0 remaining).
$ cat example.py
import numpy as np
import pandas
To both reorder imports and format the rest of the file, run the linter and the formatter:
$ uvx ruff check --select I --fix .
$ uvx ruff format .
Add Ruff to a project for ongoing sorting
For repeated use, add Ruff as a development dependency instead of running it through uvx each time:
$ uv add --dev ruff
Enable the I rule with the [tool.ruff.lint] block shown at the top of this guide, then sort imports with:
$ uv run ruff check --fix .
Because the rule is enabled in configuration, --select I is no longer needed: ruff check sorts imports as part of its normal run. To customize grouping, add an [tool.ruff.lint.isort] section, for example known-first-party = ["my_package"].
To sort imports automatically before each commit, run Ruff as a pre-commit hook.