Skip to main content

Enforces rules for the internal and external imports within your Python project.

Project description

https://img.shields.io/pypi/v/import-linter.svg Python versions https://api.travis-ci.org/seddonym/import-linter.svg?branch=master

Import Linter allows you to define and enforce rules for the internal and external imports within your Python project.

Warning: This software is currently in beta. It is undergoing active development, and breaking changes may be introduced between versions. However, due to it being a development tool (rather than something that needs to be installed on a production system), it may be suitable for inclusion in your testing pipeline. It also means we actively encourage people to try it out and submit bug reports.

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 a Python package, 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 independence contract type checks that there are no imports, in either direction, between a set of subpackages.

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 there are no dependencies between myproject.foo and myproject.bar, so we will use the independence contract type.

Create an .importlinter file in the root of your project. For example:

[importlinter]
root_package = myproject

[importlinter:contract:1]
name=Foo and bar are decoupled
type=independence
modules=
    myproject.foo
    myproject.bar

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 and bar are decoupled BROKEN

Contracts: 1 broken.


----------------
Broken contracts
----------------

Foo and bar are decoupled
-------------------------

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-1.0b2.tar.gz (15.2 kB view details)

Uploaded Source

File details

Details for the file import-linter-1.0b2.tar.gz.

File metadata

  • Download URL: import-linter-1.0b2.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.1

File hashes

Hashes for import-linter-1.0b2.tar.gz
Algorithm Hash digest
SHA256 e2d8482f83455fec9eef588b34e2e0f546434910e2b9031b818d0580f55895d7
MD5 ad95453646b5ad37252931d26d123f9a
BLAKE2b-256 e09fc5fbd99c296c8e28cea118e8eac9fbaf2d92d32945e38e9cfd3b9678ce5a

See more details on using hashes here.

Supported by

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