Detect import cycles in Python projects.
It:
- walks over the given packages or files,
- collects (file-based) Python modules,
- extracts import statements via
astand - computes cycles.
It indicates whether Python packages have structural weak points. Python's module finders and loaders are not taken into account.
The py-import-cycles package is available on PyPI: python3 -m pip install --user py-import-cycles
py_import_cycles --packages /path/to/project/packagepy_import_cycles --files /path/to/repo::pkg/module.pypy_import_cycles --helpfor all options
python3 -m py_import_cycles works as well.
Choose the cycle detection algorithm with --strategy:
dfs(default): one cycle per start module, found by depth-first search (fast)tarjan: the strongly connected components with more than one modulejohnson: all simple cycles; this may take very long, limit the cycle length with--length-bound
Single import statements can be ignored via # py-import-cycles: ignore:
import a # py-import-cycles: ignore
# py-import-cycles: ignore
import b- The number of found cycles is printed to stderr, the cycles themselves with
--verbose. - A log file is written to
~/.local/py-import-cycles/outputs/(see--outputs-folderand--outputs-filename). --graphwrites the cycles as Graphviz file to the same folder and opens it.--sarif FILEwrites the cycles as SARIF toFILE, or to stdout ifFILEis-.
The exit status is 1 if more cycles than --threshold (default: 0) are found, 0 otherwise.