Install & Compatibility
Where this runs
tested against v6.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.915 runs
installs and imports cleanly · install 0.0s · import 0.010s · 73.6MB
glibcpy 3.10–3.915 runs
installs and imports cleanly · install 2.1s · import 0.007s · 19MB
45MB installed
● package 45MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
cc_visit
✓ from radon.complexity import cc_visit
For calculating Cyclomatic Complexity programmatically.
mi_visit
✓ from radon.metrics import mi_visit
For calculating Maintainability Index programmatically.
analyze
✓ from radon.raw import analyze
For calculating raw metrics (SLOC, comments, etc.) programmatically.
h_visit
✓ from radon.halstead import h_visit
For calculating Halstead metrics programmatically.
This quickstart demonstrates how to use `radon.complexity.cc_visit` to programmatically analyze Python code for cyclomatic complexity. It shows how to pass a string of code and iterate through the detected code blocks, printing their name, complexity score, and rank.
from radon.complexity import cc_visit
def my_complex_function(x, y):
if x > 0 and y < 10:
return x * y
elif x < 0 or y > 20:
return x + y
else:
return 0
code = """
def example_function(a, b):
if a > 0:
if b < 0:
return a - b
else:
return a + b
else:
return 0
"""
# Analyze a string of code
complexity_blocks = cc_visit(code)
for block in complexity_blocks:
print(f"Function: {block.name}, Complexity: {block.real_cc}, Rank: {block.rank}")
# Or analyze a function object directly (Radon works on AST nodes, so you'd typically parse a file or string first)
# For a direct function object like my_complex_function, you would need to parse its source:
import inspect
function_code = inspect.getsource(my_complex_function)
complexity_for_func_obj = cc_visit(function_code)
for block in complexity_for_func_obj:
print(f"Function: {block.name}, Complexity: {block.real_cc}, Rank: {block.rank}")
radon --version
Errors
Common errors & fixes
radon: command not found
The 'radon' executable is not found in your system's PATH, usually because the package is not installed or its installation directory is not in PATH.
fixInstall the radon package using pip: `pip install radon`. If already installed, ensure your Python environment's script directory (e.g., ~/.local/bin or C:\PythonXX\Scripts) is correctly added to your system's PATH environment variable.
ImportError: cannot import name 'cc_visit' from 'radon'
The function 'cc_visit' is not directly available under the top-level 'radon' package; it resides within the 'radon.complexity' submodule.
fixChange the import statement to explicitly import from the correct submodule: `from radon.complexity import cc_visit`.
TypeError: expected string or bytes-like object, got _io.TextIOWrapper
A function like `radon.complexity.cc_visit` expects the source code as a string or bytes, but an open file object (e.g., from `open()`) was passed directly.
fixRead the content of the file into a string before passing it to the radon function: `with open('your_file.py', 'r') as f: code = f.read(); cc_visit(code)`. Upgrade
Version history
6.0.1latest on PyPI · released Mar 26, 2023
Audit
Dependencies
mandorequiredStrictly required for the command-line interface.
coloramaoptionalUsed for colored output in the CLI, but not strictly required; output will simply not be colored if absent.
tomlioptionalRequired for pyproject.toml configuration support on Python versions older than 3.11.
nbformatoptionalRequired for analyzing Jupyter Notebook (.ipynb) files.