Skip to main content

Enforces rules for the imports within and between Python packages.

Project description Python versions CI Status

Import Linter allows you to define and enforce rules for the imports within and between Python packages.


Import Linter is a command line tool to check that you are following a self-imposed architecture within your Python project. It does this by analysing the imports between all the modules in one or more Python packages, and compares this against a set of rules that you provide in a configuration file.

The configuration file contains one or more ‘contracts’. Each contract has a specific type, which determines the sort of rules it will apply. For example, the forbidden contract type allows you to check that certain modules or packages are not imported by parts of your project.

Import Linter is particularly useful if you are working on a complex codebase within a team, when you want to enforce a particular architectural style. In this case you can add Import Linter to your deployment pipeline, so that any code that does not follow the architecture will fail tests.

If there isn’t a built in contract type that fits your desired architecture, you can define a custom one.

Quick start

Install Import Linter:

pip install import-linter

Decide on the dependency flows you wish to check. In this example, we have decided to make sure that has dependencies on neither nor myproject.baz, so we will use the forbidden contract type.

Create an .importlinter file in the root of your project to define your contract(s). In this case:

root_package = myproject

name=Foo doesn't import bar or baz

Now, from your project root, run:


If your code violates the contract, you will see an error message something like this:

Import Linter


Analyzed 23 files, 44 dependencies.

Foo doesn't import bar or baz BROKEN

Contracts: 1 broken.

Broken contracts

Foo doesn't import bar or baz
----------------------------- is not allowed to import

- -> (l.16) -> (l.1) -> (l.3)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

import-linter-2.0.tar.gz (28.7 kB view hashes)

Uploaded Source

Built Distribution

import_linter-2.0-py3-none-any.whl (41.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page