Complexipy is an extremely fast Python library, written in Rust, designed to calculate the cognitive complexity of Python files. Unlike cyclomatic complexity, it focuses on measuring how difficult code is for humans to read and understand, identifying hard-to-maintain sections. It operates as both a command-line interface (CLI) tool and a Python API, facilitating integration into development workflows, CI/CD pipelines, and pre-commit hooks. The current version is 5.2.0, with an active release cadence, frequently adding new features and improvements.
pip install complexipyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `complexipy` both by analyzing a Python code string directly and by analyzing a Python file on disk. It imports `file_complexity` and `code_complexity` to get detailed cognitive complexity metrics for functions within the given code.
Review existing codebases and adjust complexity thresholds (`--max-complexity-allowed`) or refactor high-complexity functions. Consider using `--snapshot-create` (v5.0.0+) to establish a baseline for existing complexity.
Be aware that complexity scores for functions with extensive conditional logic might increase. Update your `pyproject.toml` configuration or CLI flags to reflect new acceptable thresholds if necessary. Refactor deeply nested or highly branched logic.
Understand the snapshot behavior for CI/CD. Use `--snapshot-ignore` to bypass snapshot comparisons temporarily or review and commit updated snapshots (`complexipy-snapshot.json`) when improvements are made. Manage `.complexipy_cache` if you need to clear old change tracking data.
Update your `pyproject.toml` or `complexipy.toml` configuration and CLI commands to use `output-format = "json"` or `--output-format json` respectively, for generating reports in specific formats.
Ensure `complexipy` is installed with `pip install complexipy`. If using a virtual environment, activate it before running the command. Verify the Python scripts directory is in your system's PATH.
To prevent failure on high complexity, use `complexipy . --ignore-complexity` to report all functions without failing, or set a higher threshold with `complexipy . --max-complexity-allowed <value>`. To disable the error exit entirely, use `complexipy . --max-complexity-allowed 0`.
Double-check the path provided to `complexipy`. Ensure it is correct and accessible from your current working directory. Use absolute paths for clarity or navigate to the correct directory before execution.