import-deps
import-deps is a Python library and CLI tool designed to find and analyze import dependencies within Python modules and packages. It helps identify issues like circular dependencies, re-imports, and inner imports. It is built upon Python's standard `ast` module, ensuring that analyzed modules are not executed. The library is actively maintained, with version 0.5.1 released in January 2026, indicating a regular release cadence.
Warnings
- breaking Dropped support for Python 3.8 and 3.9 in version 0.4.0. Users on these Python versions must use an older version of `import-deps`.
- breaking The `--check` CLI flag behavior was unified and changed in version 0.5.0. Previously, it might have had a default check, but now it supports optional types like `--check=all`, `--check=circular`, `--check=reimports`, or `--check=inner`. The behavior of a bare `--check` flag might be different from prior versions.
- gotcha When using `--check=reimports`, `__init__.py` files are explicitly whitelisted by default, as they commonly re-export symbols for a cleaner public API. This means re-imports in `__init__.py` files will not trigger a check failure.
- gotcha Version 0.5.0 added support for analyzing multiple paths (files and/or directories) in a single command. Older versions might have handled single paths differently or lacked this flexibility.
Install
-
pip install import-deps
Imports
- ModuleSet
from import_deps import ModuleSet
- ast_imports
from import_deps.analyzer import ast_imports
Quickstart
import os
import pathlib
import subprocess
# Create a dummy package for demonstration
package_dir = "my_dummy_package"
os.makedirs(f"{package_dir}/sub_module", exist_ok=True)
with open(f"{package_dir}/__init__.py", "w") as f:
f.write("from .module_a import func_a\n")
with open(f"{package_dir}/module_a.py", "w") as f:
f.write("import os\nfrom .sub_module.module_b import func_b\ndef func_a(): pass\n")
with open(f"{package_dir}/sub_module/module_b.py", "w") as f:
f.write("import sys\ndef func_b(): pass\n")
# Run import-deps CLI to analyze the package and output JSON
print(f"\nAnalyzing '{package_dir}' with import-deps CLI:\n")
cmd = ["import_deps", package_dir, "--json"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
print(result.stdout)
# Clean up dummy package
import shutil
shutil.rmtree(package_dir)