Skip to content

relative-imports (TID252)

Added in v0.0.169 · Related issues · View source

Derived from the flake8-tidy-imports linter.

Fix is sometimes available.

What it does

Checks for relative imports.

Why is this bad?

Absolute imports, or relative imports from siblings, are recommended by PEP 8:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved...

import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example

However, explicit relative imports are an acceptable alternative to absolute imports, especially when dealing with complex package layouts where using absolute imports would be unnecessarily verbose:

from . import sibling
from .sibling import example

Example

from .. import foo

Use instead:

from mypkg import foo

Options

Fix safety

When available, this rule's fix is always marked as unsafe because Ruff infers the absolute import path from the file's location and configured package roots, while Python resolves relative imports from the module's runtime package. If those differ, the rewritten import can fail or resolve to a different module. The fix may also remove comments attached to the import statement.