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
Project self-graph (SVG)
Current dependency graph of src/import_cruiser (generated by import-cruiser itself):
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
Quick start: generate HTML/SVG for your repo
Use this when you want a graph fast, without setting up rules first.
# 1) Install tool
pip install import-cruiser
# 2) From your repository root, export an interactive HTML graph
import-cruiser export . --format html --output deps.html
# 3) Export SVG directly
import-cruiser export . --format svg --output deps.svg
# 4) (Optional) Export DOT too, for custom Graphviz rendering
import-cruiser export . --format dot --output deps.dot
Useful filters for larger repos:
# Focus on src/ and skip tests
import-cruiser export . --format html --include-path '^src/' --exclude-path '/tests/' --output deps-src.html
# Focus on a package/module prefix
import-cruiser export . --format svg --include '^myapp\.' --output deps-myapp.svg
Tip: HTML is best for exploration (hover/pan/zoom), SVG is best for embedding in docs.
CI usage (GitHub Actions)
name: dependency-graph
on:
workflow_dispatch:
pull_request:
jobs:
graph:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install import-cruiser
- run: import-cruiser export . --format html --output deps.html
- run: import-cruiser export . --format svg --output deps.svg
- uses: actions/upload-artifact@v4
with:
name: dependency-graph
path: |
deps.html
deps.svg
Command overview
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
# Emit linter-style output for editor/CI integrations
import-cruiser validate ./myproject --config import-cruiser.json --output-format flake8
import-cruiser validate ./myproject --config import-cruiser.json --output-format pylint
import-cruiser validate ./myproject --config import-cruiser.json --output-format github
Linter integration output modes
validate supports multiple output formats via --output-format:
json(default): full graph + violations + cyclesflake8:path:line:col: CODE messagepylint:path:line: [CODE] messagegithub: GitHub Actions annotations (::error file=...::...)
This makes it easy to plug import-cruiser into common linting pipelines.
In practice, the most-used Python lint tools are typically Ruff, Flake8, and Pylint.
For those stacks, flake8/pylint output modes are editor-friendly, and github
is ideal for GitHub Actions logs.
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
Architecture enforcement (pre-commit)
This repository uses import-cruiser itself in pre-commit to enforce dependency boundaries:
- Config file:
import-cruiser.json - Hook:
import-cruiser-architecture - Command:
PYTHONPATH=src python3 -m import_cruiser.cli validate src --config import-cruiser.json --strict --output-format pylint
This gives linter-friendly output and fails commits when architectural rules are violated.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file import_cruiser-0.2.6.tar.gz.
File metadata
- Download URL: import_cruiser-0.2.6.tar.gz
- Upload date:
- Size: 23.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb8bfd02304e4eb9a06eea4af2d8bd64a2729d82241e8e35862d28fad61cd280
|
|
| MD5 |
233d97e6945dfa0336aceaecf5674c3f
|
|
| BLAKE2b-256 |
46de7fa05e6c8ddfafcae964a3b28b437e231071e1e800975c1da61065f73c47
|
Provenance
The following attestation bundles were made for import_cruiser-0.2.6.tar.gz:
Publisher:
workflow.yml on kevin91nl/import-cruiser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
import_cruiser-0.2.6.tar.gz -
Subject digest:
eb8bfd02304e4eb9a06eea4af2d8bd64a2729d82241e8e35862d28fad61cd280 - Sigstore transparency entry: 1157430902
- Sigstore integration time:
-
Permalink:
kevin91nl/import-cruiser@dabc76d4e10243f7bb1305d57be5ec6136158635 -
Branch / Tag:
refs/tags/v0.2.6 - Owner: https://github.com/kevin91nl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@dabc76d4e10243f7bb1305d57be5ec6136158635 -
Trigger Event:
push
-
Statement type:
File details
Details for the file import_cruiser-0.2.6-py3-none-any.whl.
File metadata
- Download URL: import_cruiser-0.2.6-py3-none-any.whl
- Upload date:
- Size: 24.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db42d9583070242c23447c3e7e58243b6b7611880c8967cb8fe5ce8bd82f6179
|
|
| MD5 |
51c878555844a1099c13b05bd5c6e356
|
|
| BLAKE2b-256 |
b6c6f153b87775054463e068f2475044c7f2a447cd0de7f4ea6dd6c2400da85c
|
Provenance
The following attestation bundles were made for import_cruiser-0.2.6-py3-none-any.whl:
Publisher:
workflow.yml on kevin91nl/import-cruiser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
import_cruiser-0.2.6-py3-none-any.whl -
Subject digest:
db42d9583070242c23447c3e7e58243b6b7611880c8967cb8fe5ce8bd82f6179 - Sigstore transparency entry: 1157430955
- Sigstore integration time:
-
Permalink:
kevin91nl/import-cruiser@dabc76d4e10243f7bb1305d57be5ec6136158635 -
Branch / Tag:
refs/tags/v0.2.6 - Owner: https://github.com/kevin91nl
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@dabc76d4e10243f7bb1305d57be5ec6136158635 -
Trigger Event:
push
-
Statement type: