← Back to list

Python: Code Quality Using Radon

Radon is a Python reporting tool for code metrics, including cyclomatic complexity and the maintainability index.

Chris Alba Hamilton · 2025-06-17 13:17 · 2 claps · 1.8 min read
#python #code-quality #radon #code-complexity #cyclomatic-complexity
Open on Medium ↗

Python: Code Quality Using Radon

Radon is a Python reporting tool for code metrics, including cyclomatic complexity and the maintainability index.

How easy is some Python code to maintain? Let’s explore two reports, on a per-file and a per-function level.

Report on the maintainability index of Python code

Let’s use Radon to report on the complexity of Python source code files, with a per-file result graded from A to C.

*radon mi .py**

Example output (using artificial file names for this article):

  • file1.py — A
  • file2.py — A
  • file3.py — A
  • file4.py — A
  • file5.py — A
  • file6.py — A

The results are from A (the easiest code to maintain) to C (worst).

Maintainability Index: https://radon.readthedocs.io/en/latest/commandline.html#the-mi-command

Maintainability Index: https://radon.readthedocs.io/en/latest/commandline.html#the-mi-command

Report on cyclomatic complexity of Python code

Here’s how to create an aggregated report about how complex some Python code is, with an aggregated result from analysing the functions.

radon cc .py | awk ‘{print $5}’ | grep -v ‘.py’ | grep -v ‘^\s$’ | sort | uniq -c

Example output: 40 A 4 B 1 C Where each letter above is for a function.

The command line analyses all functions within the Python files in the current folder and computes cyclomatic complexity, assigning a letter (A is best, F is worst) to each function.

Cyclomatic Complexity: https://radon.readthedocs.io/en/latest/commandline.html#the-cc-command

Cyclomatic Complexity: https://radon.readthedocs.io/en/latest/commandline.html#the-cc-command

Functions with an A grade are easier for humans to understand and will be easier to write comprehensive automated tests against.

To see the Python function names (e.g. to improve the B’s and C’s to become A’s — if that is realistic), simplify the command line:

*radon cc .py**

What the bash script does

Here’s a breakdown of the following bash scripting:

radon cc .py | awk ‘{print $5}’ | grep -v ‘.py’ | grep -v ‘^\s$’ | sort | uniq -c

awk ‘{print $5}’ grabs the grade at the end of each line: A, B, C, D, E, or F.

grep -v ‘.py’ ignores the output lines with Python filenames — because Radon does not include grades (A, B, C, D, E or F) in them

*grep -v ‘^\s$’** will remove empty lines, including those with only spaces or tabs.

sort | uniq -c will sort the grades (A, B, C, D, E or F) into alphabetical order and then count the number of lines.

Have fun!


메타데이터
post_id
6f2722ea1eb4
slug
python-code-quality-using-radon-6f2722ea1eb4
url
https://medium.com/@chris.alba.hamilton/python-code-quality-using-radon-6f2722ea1eb4
canonical_url
https://medium.com/@chris.alba.hamilton/python-code-quality-using-radon-6f2722ea1eb4
author_url
https://medium.com/@chris.alba.hamilton
status
ok
fetched_at
2026-07-17 16:42:39