{"id":2256,"library":"radon","title":"Radon","description":"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.","status":"active","version":"6.0.1","language":"en","source_language":"en","source_url":"https://github.com/rubik/radon","tags":["code quality","metrics","static analysis","linter","cyclomatic complexity","maintainability index","halstead metrics"],"install":[{"cmd":"pip install radon","lang":"bash","label":"Basic Installation"},{"cmd":"pip install radon[toml]","lang":"bash","label":"For pyproject.toml support (Python < 3.11)"},{"cmd":"pip install nbformat","lang":"bash","label":"For Jupyter Notebooks support"}],"dependencies":[{"reason":"Strictly required for the command-line interface.","package":"mando","optional":false},{"reason":"Used for colored output in the CLI, but not strictly required; output will simply not be colored if absent.","package":"colorama","optional":true},{"reason":"Required for pyproject.toml configuration support on Python versions older than 3.11.","package":"tomli","optional":true},{"reason":"Required for analyzing Jupyter Notebook (.ipynb) files.","package":"nbformat","optional":true}],"imports":[{"note":"For calculating Cyclomatic Complexity programmatically.","symbol":"cc_visit","correct":"from radon.complexity import cc_visit"},{"note":"For calculating Maintainability Index programmatically.","symbol":"mi_visit","correct":"from radon.metrics import mi_visit"},{"note":"For calculating raw metrics (SLOC, comments, etc.) programmatically.","symbol":"analyze","correct":"from radon.raw import analyze"},{"note":"For calculating Halstead metrics programmatically.","symbol":"h_visit","correct":"from radon.halstead import h_visit"}],"quickstart":{"code":"from radon.complexity import cc_visit\n\ndef my_complex_function(x, y):\n    if x > 0 and y < 10:\n        return x * y\n    elif x < 0 or y > 20:\n        return x + y\n    else:\n        return 0\n\ncode = \"\"\"\ndef example_function(a, b):\n    if a > 0:\n        if b < 0:\n            return a - b\n        else:\n            return a + b\n    else:\n        return 0\n\"\"\"\n\n# Analyze a string of code\ncomplexity_blocks = cc_visit(code)\nfor block in complexity_blocks:\n    print(f\"Function: {block.name}, Complexity: {block.real_cc}, Rank: {block.rank}\")\n\n# Or analyze a function object directly (Radon works on AST nodes, so you'd typically parse a file or string first)\n# For a direct function object like my_complex_function, you would need to parse its source:\nimport inspect\nfunction_code = inspect.getsource(my_complex_function)\ncomplexity_for_func_obj = cc_visit(function_code)\nfor block in complexity_for_func_obj:\n    print(f\"Function: {block.name}, Complexity: {block.real_cc}, Rank: {block.rank}\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade Python to 2.7+ or 3.4+. For older Python 2.x projects, pin `radon<2.0.0`.","message":"Python 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.","severity":"breaking","affected_versions":"< 2.0.0"},{"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.","message":"On Windows, if your Python files contain Unicode characters and your system's default encoding is not UTF-8, Radon might encounter encoding issues.","severity":"gotcha","affected_versions":"All"},{"fix":"Review your CI/CD thresholds if upgrading from pre-3.0.0 versions, as complexity scores for classes might change.","message":"Radon'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.","severity":"breaking","affected_versions":"< 3.0.0"},{"fix":"Ensure `flake8` version is 3.x or higher when using Radon's Flake8 plugin.","message":"Support 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.","severity":"breaking","affected_versions":"5.0.1+"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}