Registry / testing / radon
library6.0.1pypypi✓ verified 26d ago

Radon is a Python tool that computes various metrics from the source code, including McCabe's cyclomatic complexity, raw metrics (SLOC, comment lines, blank lines), Halstead metrics, and the Maintainability Index. It can be used via the command line or programmatically through its API. The current version is 6.0.1, released in March 2023, and it maintains a consistent release cycle with several minor and patch updates annually.

pip install radon
INSTALL
IMPORT
SIG · RADON
R
radon
testingpythonv6.0.1
Install
2.1s avg
Import
9ms
Disk
45MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 0.010s · 73.6MB
glibc
py 3.103.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
Debug
Known issues
breakingPython 2.6 is no longer supported since Radon version 2.0.0. Projects still using Python 2.6 will need to use an older Radon version (<= 1.5.0) or upgrade their Python environment.
fix
Upgrade Python to 2.7+ or 3.4+. For older Python 2.x projects, pin `radon<2.0.0`.
affects: < 2.0.0
gotchaOn Windows, if your Python files contain Unicode characters and your system's default encoding is not UTF-8, Radon might encounter encoding issues.
fix
Set the `RADONFILESENCODING` environment variable to `UTF-8` before running Radon, e.g., `set RADONFILESENCODING=UTF-8` in CMD or `$env:RADONFILESENCODING='UTF-8'` in PowerShell.
affects: All
breakingRadon's total complexity calculation for classes was fixed in version 3.0.0, which might lead to different (more accurate) results compared to previous versions.
fix
Review your CI/CD thresholds if upgrading from pre-3.0.0 versions, as complexity scores for classes might change.
affects: < 3.0.0
breakingSupport for `flake8 < 3.x` was dropped in version 5.0.1, leading to the removal of the `flake8-polyfill` dependency. This was temporarily added back in 4.5.2 and then definitively removed.
fix
Ensure `flake8` version is 3.x or higher when using Radon's Flake8 plugin.
affects: 5.0.1+
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.
fix
Install 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.
fix
Change 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.
fix
Read 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.
Agent activity
17 hits · last 30 days
node
12
Amazon
1
Resources
radon — pip install radon · libregistry