Skip to content

PY-IMPO0001 · internal_relative_import

Detect an absolute import of a module owned by the current project package.

This is a deterministic rule for python. Read its implementation.

Index Python modules below the configured source roots, including namespace-package prefixes that have no __init__.py. Inspect selected files for absolute import and from statements whose resolved module belongs to the same top-level project package as the importing module. Derive the relative level from the current package and longest common dotted prefix. The Boolean result identifies one qualifying statement. The default policy prefers relative imports.

Each finding records the imported project modules and source range. A safe UTF-8 text edit is attached when the statement is single-line, the relative level is unique, and all local names remain unchanged. Aliased module imports can be rewritten as from imports. Unaliased dotted imports are reported without an edit because they bind the package root rather than the final module name.

Keep absolute imports across different top-level packages, for unresolved modules, or when a public executable intentionally supports direct invocation outside its package. Relative imports already in use, wildcard imports, standalone top-level modules, mixed import lists, ignored files receive no fact from discovery and therefore no automatic edit. Package-root from imports are supported, while a bare import package has no binding-preserving relative equivalent.

Bad

Inside `acme/features/service.py`, `from acme.models import User` and
`import acme.tools.formatting as formatting` are internal absolute imports.
Good

from ...models import User, from ...tools import formatting as formatting, and import httpx preserve an explicit package boundary.

  • Cites “PEP 328, Imports and Relative Imports”. Open reference
  • Cites “The Python Language Reference”, the import statement. Open reference
  • Cites “The Python Language Reference”, Namespace packages. Open reference