Skip to content

import-layout

Prose holds code to a line-length budget but leaves imports exempt by default, so a from a.deeply.nested.module import x, y, z, ... runs past the margin however wide its roster grows.

gives a long from-import a deliberate shape once it overflows a dedicated import budget, rewriting it into a run of from ... import ... statements. Each statement repeats the module prefix and greedily packs as many alphabetized names as fit before the next line opens, so the imported names begin at the column the eye reaches after import on every line and a deep module path never drives them rightward.

The rule acts on a single-line from ... import ... only. A from ... import *, a from-import already within budget, and a multi-line (parenthesized or backslash-continued) import stay untouched, and a lone name whose own line still overflows keeps its place rather than splitting further. Pair with

to sort each roster before the split packs it, and with to align the import keyword across the resulting run, which already carries one identical prefix per line.

Configuration

KeyTypeDefaultMeaning
enabledbooltrueToggle the rule on or off

The wrap budget comes from the top-level import-line-length key (default 120), governing the import wrap independently of code-line-length. An import is a roster

already orders, so it stays scannable at a width where dense expression code would not, earning more horizontal room before a wrap pays off. Setting import-line-length to false drops the dedicated budget, so the import wrap falls back to code-line-length.

The Canonical Case

A from ... import ... whose names overrun import-line-length splits into a run of repeated-prefix statements, each greedily packing the alphabetized names up to the budget before the next line opens.

python
from pkg.sub import alpha, beta, delta
from pkg.sub import epsilon, gamma

More Examples

A relative import folds its leading dots into the repeated prefix, so every split line opens with the same from ..pkg import anchor.

An as-aliased name keeps its alias when the line splits, so each packed statement repeats the module prefix and holds every name as alias pair intact.

A from-import nested in a block splits with every continuation line carrying the block indent, and the budget counts the indent column so each packed line still fits.

No Change

A single name whose own from ... import ... line still overruns the budget stays in place rather than splitting further, since one name has nowhere left to break.

No Change

A from ... import * carries one name and passes through unchanged, even past the budget, because a star import has nothing to pack.

No Change

A from ... import ... that already fits the import budget keeps its single line, leaving the split for the genuinely long rosters.