Skip to main content

Enforces rules for the imports within and between Python packages.

Project description

https://img.shields.io/pypi/v/import-linter.svg Python versions CI Status

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

Overview

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 myproject.foo has dependencies on neither myproject.bar 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:

[importlinter]
root_package = myproject

[importlinter:contract:1]
name=Foo doesn't import bar or baz
type=forbidden
source_modules=
    myproject.foo
forbidden_modules=
    myproject.bar
    myproject.baz

Now, from your project root, run:

lint-imports

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

=============
Import Linter
=============

---------
Contracts
---------

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
-----------------------------

myproject.foo is not allowed to import myproject.bar:

-   myproject.foo.blue -> myproject.utils.red (l.16)
    myproject.utils.red -> myproject.utils.green (l.1)
    myproject.utils.green -> myproject.bar.yellow (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.1.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

import_linter-2.1-py3-none-any.whl (41.4 kB view details)

Uploaded Python 3

File details

Details for the file import_linter-2.1.tar.gz.

File metadata

  • Download URL: import_linter-2.1.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for import_linter-2.1.tar.gz
Algorithm Hash digest
SHA256 393fadb2e91304c22c5ceab575213ed2b6a794abc7bd05ab345f2430930b2eae
MD5 b06901b48c50fdcb8cfd20cd35104f39
BLAKE2b-256 5ed3546d27e9390061cbc692541612bbaa3f69a3ad23f592cfbfefe0517ff5b0

See more details on using hashes here.

File details

Details for the file import_linter-2.1-py3-none-any.whl.

File metadata

  • Download URL: import_linter-2.1-py3-none-any.whl
  • Upload date:
  • Size: 41.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for import_linter-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 324d65035f0252a8e432f60256b0d0d32d8d5d6e4f8fd29716688b09d7a2217a
MD5 f9c927c511af7c46701540a92807bebd
BLAKE2b-256 d838137d5310c52c1b79d1588afa2ce5038cb3a10dbfd1a642962f3bc3ccbe61

See more details on using hashes here.

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