isort & Import Organization
Consistent import order makes diffs readable and prevents circular import surprises. Ruff's isort rules (I) handle this automatically - no separate isort install needed.
Search across all documentation pages
Consistent import order makes diffs readable and prevents circular import surprises. Ruff's isort rules (I) handle this automatically - no separate isort install needed.
[tool.ruff.lint.isort]
known-first-party = ["myapp"]
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]uv run ruff check --fix . # auto-sort importsWhen to reach for this:
# Correct order (after ruff fix)
from __future__ import annotations
import os
from pathlib import Path
import httpx
from pydantic import BaseModel
from myapp.config import settings
from myapp.models import User[tool.ruff.lint.isort]
known-first-party = ["myapp", "myapp_utils"]
force-single-line = false
lines-after-imports = 2What this demonstrates:
known-first-party tells ruff which packages are yours--fix reorders without manual editing| Group | Examples |
|---|---|
future | from __future__ import annotations |
| standard-library | os, pathlib, typing |
| third-party | httpx, fastapi, pandas |
| first-party | your installed packages |
| local-folder | relative imports in scripts |
# Prefer absolute imports in libraries
from myapp.services import billing
# Relative imports ok within a package
from .models import Userknown-first-party - your packages sorted as third-party. Fix: list all first-party package names.from x import *) - lint flags and obscures origin. Fix: explicit imports except __init__.py re-exports.TYPE_CHECKING.__init__.py - re-exports confuse isort. Fix: per-file override or explicit __all__.| Alternative | Use When | Don't Use When |
|---|---|---|
| Manual ordering | 1-2 file project | Any team project |
| Standalone isort | Not using ruff | Already on ruff |
| No sorting | - | Never for production |
No. Ruff implements isort rules natively.
force-single-line = true in [tool.ruff.lint.isort].
import pandas as pd stays in the third-party group. isort preserves aliases.
Ruff's isort places them correctly. Use if TYPE_CHECKING: blocks as normal.
Per-file ignore: "__init__.py" = ["I001"] if re-exports need manual order.
PEP 8 recommends two blank lines before top-level functions. lines-after-imports = 2 enforces this.
List all workspace package names in known-first-party.
No. This is readability and convention only.
VS Code ruff extension with "source.organizeImports.ruff": "explicit" or format on save.
Ruff's import-sorting violation code. Fixed with ruff check --fix.
Stack versions: This page was written for Python 3.14.0, FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 19, 2026