Skip to content

unsorted-imports (I001)

Added in v0.0.110 · Category: style · Related issues · View source

Derived from the isort linter.

Fix is sometimes available.

What it does

De-duplicates, groups, and sorts imports based on the provided isort settings.

By default, imports are categorized into the following sections in order:

  1. future (__future__)
  2. standard-library (e.g., sys, os, json)
  3. third-party (e.g., numpy, pandas)
  4. first-party (modules within the current project or package)
  5. local-folder (relative imports starting with ., e.g., from . import utils)

With the default settings, each section is separated by a blank line. Within a section, Ruff generally places import statements before from imports and sorts each group by module name.

Ruff defaults to two blank lines between a top-level import block and a function or class definition, and one blank line before other top-level statements. In stub files (.pyi), the default is one blank line in either case.

See the FAQ for more information on how Ruff determines which imports are first-party and for how Ruff compares to isort more broadly.

Why is this bad?

Consistency is good. Use a common convention for imports to make your code more readable and idiomatic.

Example

import pandas
import numpy as np
import sys


def main(): ...

Use instead:

import sys

import numpy as np
import pandas


def main(): ...

Options

See the isort settings for all available configuration options.

References