Skip to main content

Analyze, validate, and visualize Python import dependencies — like dependency-cruiser for Python.

Project description

import-cruiser

Analyze, validate, and visualize Python import dependencies.

import-cruiser is a CLI tool for Python projects inspired by dependency-cruiser. It parses Python import statements, builds a dependency graph, detects circular dependencies, validates the graph against configurable rules, and can export the results as JSON or DOT (Graphviz) format.


Features

  • 🔍 Parse all Python imports in a project directory
  • 🔄 Detect circular dependencies automatically
  • Validate dependencies against user-defined JSON rules
  • 📊 Export dependency graphs as JSON or DOT/Graphviz
  • 🖥️ CLI with three subcommands: analyze, validate, export

Installation

With pip

pip install import-cruiser

From source (with Poetry)

git clone https://github.com/kevin91nl/import-cruiser.git
cd import-cruiser
poetry install

CLI Usage

import-cruiser [OPTIONS] COMMAND [ARGS]...

Commands:
  analyze   Analyze imports and output results.
  export    Export the dependency graph.
  validate  Validate dependencies against rules.

analyze

Scan a Python project and output a JSON or DOT dependency report.

# JSON report (default)
import-cruiser analyze ./myproject

# DOT format for Graphviz
import-cruiser analyze ./myproject --format dot

# Write to file
import-cruiser analyze ./myproject --output report.json

JSON output structure:

{
  "summary": {
    "modules": 12,
    "dependencies": 18,
    "cycles": 0,
    "violations": 0
  },
  "modules": [...],
  "dependencies": [...],
  "cycles": [],
  "violations": []
}

validate

Validate dependencies against rules defined in a JSON configuration file.

import-cruiser validate ./myproject --config import-cruiser.json

# Exit non-zero if there are any violations (useful in CI)
import-cruiser validate ./myproject --config import-cruiser.json --strict

export

Export the dependency graph to DOT format (compatible with Graphviz).

import-cruiser export ./myproject --output graph.dot

# Render with Graphviz
dot -Tsvg graph.dot -o graph.svg

Configuration

Create a import-cruiser.json file to define dependency rules:

{
  "rules": [
    {
      "name": "no-circular",
      "severity": "error",
      "from": { "path": "myapp\\.ui" },
      "to":   { "path": "myapp\\.data" },
      "allow": false
    },
    {
      "name": "allow-utils",
      "severity": "warn",
      "from": {},
      "to": { "path": "myapp\\.utils" },
      "allow": true
    }
  ],
  "options": {
    "include_external": false
  }
}

Rule schema

Field Type Required Description
name string Unique rule identifier
severity string "error", "warn", or "info"
from object Source module pattern (see Pattern object below)
to object Target module pattern (see Pattern object below)
allow boolean true (default) = allowed; false = forbidden

Pattern object

Field Type Description
path string Regular expression matched against the module name

An empty pattern object {} matches all modules.


Examples

Detect circular dependencies

import-cruiser analyze ./myproject
# Check the "cycles" key in the JSON output

Enforce layered architecture

{
  "rules": [
    {
      "name": "no-data-to-ui",
      "severity": "error",
      "from": { "path": "\\.data" },
      "to":   { "path": "\\.ui" },
      "allow": false
    }
  ]
}
import-cruiser validate ./myproject --config import-cruiser.json --strict

Visualize dependencies

import-cruiser export ./myproject --output deps.dot
dot -Tpng deps.dot -o deps.png
open deps.png

Development

# Install dev dependencies
poetry install

# Run tests
poetry run pytest

# Run tests with coverage
poetry run pytest --cov

License

MIT

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_cruiser-0.1.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

import_cruiser-0.1.0-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file import_cruiser-0.1.0.tar.gz.

File metadata

  • Download URL: import_cruiser-0.1.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for import_cruiser-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3734f0f3c5a61abd30ffff9d90a1e2034ed5e5ca5b9bf0258cfa8445d02248b7
MD5 9d07355d68798923352a761f685e1a48
BLAKE2b-256 18f6fa1e7c40e0341b2a86e9e2e65f47377c986cdf0e469e3421356b29dbcdda

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_cruiser-0.1.0.tar.gz:

Publisher: workflow.yml on kevin91nl/import-cruiser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file import_cruiser-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: import_cruiser-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for import_cruiser-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c176db4109460d6b28401853dec3db30fa1dfea1db91de374340988fef6374db
MD5 8d182b17afd9deb31b6ef4ae12459f62
BLAKE2b-256 28a1548e59f49fbdff1b0febeece4f3c4baa87b9b4879c6e203fb08728985866

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_cruiser-0.1.0-py3-none-any.whl:

Publisher: workflow.yml on kevin91nl/import-cruiser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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