Import Linter

2.11 · active · verified Thu Apr 09

Import Linter is a command-line tool designed to lint your Python architecture by imposing constraints on the imports between your Python modules. It analyzes imports against a set of rules defined in a configuration file, helping to enforce specific architectural styles in complex codebases. The library also provides a browser-based user interface for exploring the architecture of any Python package. It is actively developed, with the current version being 2.11.

Warnings

Install

Imports

Quickstart

Install `import-linter`, then create a `.importlinter` file (or `pyproject.toml`/`setup.cfg`) in your project's root. Define your contracts, specifying rules like 'forbidden' or 'layers'. Finally, run `lint-imports` from your terminal to check for architectural violations. The example demonstrates a forbidden contract preventing imports from `myproject.services` into `myproject.domain`.

# myproject/domain/__init__.py
# (empty file)

# myproject/domain/models.py
# (some model code)

# myproject/services/__init__.py
# (empty file)

# myproject/services/users.py
# (some service code)

# .importlinter (create this file in your project root)
[importlinter]
root_package = myproject

[importlinter:contract:domain-no-services]
name = Domain layer must not import from services layer
type = forbidden
source_modules = myproject.domain
forbidden_modules = myproject.services

# Run from your project root in the terminal:
# lint-imports

view raw JSON →