Python Dependency Impact Analyzer
Project description
impscope — Python Dependency Impact Analyzer
A small, fast tool to analyze Python file dependencies and assess the impact of changes. Pure standard library, no external deps.
[English | 简体中文]
Features
- Impact analysis for a given file (direct & indirect dependents)
- Not-imported files report (files not imported by any other file)
- Dependency graph (most/least depended files, with sorting)
- Codebase statistics (totals)
- Exclude by glob patterns (e.g.,
--exclude "tests/*") - Impact since a Git commit (
since <commit>) — union blast radius of changed files - Text and JSON output formats
- Source roots support (
--source-root,--include-outside-roots) - Strict/heuristic import resolution (
--strict-resolution) - Fast & lightweight (pure Python, AST-based)
- Python 3.7+
Installation
pip install impscope
Or install from source:
git clone https://github.com/zh-he/impscope.git
cd impscope
pip install -e .
Quick Start
# Show dependency statistics (brief overview)
impscope stats
# Check the impact of changing a specific file
impscope impact models.py
# List files that are not imported by any other file
impscope unimported
# Show dependency graph (descending by default)
impscope graph
# Show least depended files
impscope graph --sort asc
# Analyze a specific directory and exclude tests/migrations
impscope stats --path src/ --exclude "tests/*" --exclude "*/migrations/*"
# Impact of files changed since last commit (Git required)
impscope since HEAD
# JSON output (great for CI)
impscope since main --format json
Tip: you can also run it as a module:
python -m impscope --help
Usage
impscope <command> [OPTIONS]
Commands:
impact FILE Analyze the impact of changing a specific file
unimported List files that are not imported by any other file
graph Show dependency graph (top most/least depended files)
stats Show comprehensive dependency statistics
since COMMIT Analyze union impact of Python files changed since COMMIT
(e.g., HEAD~1, <hash>, <branch>)
Global options:
--path PATH Root path to analyze (default: current directory)
--exclude GLOB Glob pattern to exclude (repeatable)
e.g., --exclude "tests/*" --exclude "*/migrations/*"
--format {text,json} Output format (default: text)
--full Do not truncate long lists in text output
--limit N Max items per list in text output when not using --full (default: 10)
--source-root DIR Treat DIR (relative to --path) as an import root (repeatable)
--include-outside-roots When --source-root is provided, also include files outside those roots
--strict-resolution Only resolve imports that exactly match indexed modules
(no parent-package fallback)
--version Show version information
--help Show help message
How It Works
impscope uses Python’s AST to:
- Parse all Python files in your project
- Extract
importandfrom ... import ...statements (including relative imports) - Map files to modules (packages via
__init__.py) - Build a dependency graph
- Compute direct & indirect dependents (impact) via BFS
It handles:
- Regular imports (
import module) - From imports (
from module import name) - Relative imports (
from .module import name,from .. import x) - Packages and
__init__.py - Circular dependencies (no infinite recursion)
- Ignored directories:
.git,.venv,__pycache__,node_modules,dist,build, etc.
Excludes
--excludepatterns are matched against relative paths (POSIX style), e.g.src/app/models.py.- You can repeat
--excludemultiple times:impscope stats --exclude "tests/*" --exclude "*/migrations/*"
Source roots
- Use
--source-rootto treat one or more directories as import roots (relative to--path):impscope stats --path . --source-root src --source-root python
- With
--include-outside-roots, files outside these roots are also scanned (module names computed relative to the project root).
Since
- Requires Git to be available, and the path to be inside a Git repo.
- Uses
git diff --name-only --diff-filter=ACMR <commit>..HEADto collect changed.pyfiles, then computes the union of direct/indirect dependents across those files. - When
--pathpoints to a subdirectory, only changes within that directory tree are considered for display and impact. --excludepatterns also apply to the changed file list for consistent reporting.
Import resolution notes (re: from u import x without dot)
- In Python 3, absolute imports are the default.
Writing
from u import xis treated as importing top-level moduleuonsys.path, notpkg.u. impscopefollows this rule:- It resolves
from u import xonly if there is a module/package nameduinside the analyzed root (--path). - If
uactually lives under a package likepkg/u.pyand there is no top-levelu,impscopewill not inferpkg.ufromfrom u import x.
- It resolves
- Heuristic fallback (non-strict mode):
- For
import pkg.sub.modit can fall back to the nearest existing parent (pkg.sub, thenpkg). - For
from pkg import *, it may map topkg(or its nearest existing parent).
- For
- Recommendations:
- Within a package, prefer explicit relative imports:
from .u import x. - Or use fully qualified absolute imports from your package root:
from pkg.u import x. - Run
impscopefrom the repository/package root so absolute imports can be mapped.
- Within a package, prefer explicit relative imports:
Note: The “least depended files” view in
graph --sort ascranks files that have at least one dependent. Files with zero dependents are listed by theunimportedcommand.
Requirements
- Python 3.7+
- No external dependencies (standard library only)
Contributing
Contributions are welcome! Please open an issue or pull request.
License
MIT License
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 impscope-0.1.0.tar.gz.
File metadata
- Download URL: impscope-0.1.0.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab5f574bf7767b1d57a69b758c7d50a55cb032fec00ecff580c4a374a20ed896
|
|
| MD5 |
ed7d1227172d4d2dc659c1523b52373f
|
|
| BLAKE2b-256 |
91ff05f5a8c78e222e49e14201c7d154d54a5dd29585af0c1a4ade261c8b40b6
|
File details
Details for the file impscope-0.1.0-py3-none-any.whl.
File metadata
- Download URL: impscope-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e6776243c23f5a05b6bd5b5ec3314580c1ac1f91e12644b45adb9868bfc09db
|
|
| MD5 |
f764c62368f7b13e47b5223b2afb7a8f
|
|
| BLAKE2b-256 |
b2f24bd1942c371dc39c54b1ba31f80751e9ade9b1178678dc22435f9869dfd1
|