Fundamentals 7 min read

How to Make Python Debugging Clearer with Icecream: A Step‑by‑Step Guide

This article explains why using plain print() for debugging can become confusing in larger Python projects and demonstrates how the Icecream library provides concise, informative output with minimal code, including installation, basic usage, custom prefixes, context information, and clean‑up techniques.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Make Python Debugging Clearer with Icecream: A Step‑by‑Step Guide

Why plain print() can be confusing

When debugging Python code in a large project, using multiple print() statements produces many interleaved outputs, making it hard to identify which line of code produced each result.

num1 = 30
num2 = 40

print(num1)
print(num2)

The output shows 30 and 40, but you must manually match each value to its variable.

Adding descriptive text to each print helps, but requires extra typing.

print("num1", num1)
print("num2", num2)

The result is clearer, yet still verbose.

01. What is Icecream?

Icecream is a third‑party Python library that makes debugging output clearer with minimal code.

pip install icecream

Basic usage

from icecream import ic

def plus_five(num):
    return num + 5

ic(plus_five(4))
ic(plus_five(5))

Output:

ic| plus_five(4): 9
ic| plus_five(5): 10

Icecream shows both the function call and its result.

02. Checking execution flow

from icecream import ic

def hello(user: bool):
    if user:
        print("I'm user")
    else:
        print("I'm not user")

hello(user=True)

Using Icecream eliminates the need for extra print text:

from icecream import ic

def hello(user: bool):
    if user:
        ic()
    else:
        ic()

hello(user=True)

Output:

ic| ice_1.py:5 in hello() at 02:34:41.391

This indicates that line 5 in hello() was executed while line 7 was not.

03. Custom prefixes

from datetime import datetime
from icecream import ic
import time

def time_format():
    return f"{datetime.now()}|> "

ic.configureOutput(prefix=time_format)

for _ in range(3):
    time.sleep(1)
    ic('Hello')

Output shows the timestamp before each message:

2021-01-24 10:38:23.509304|> 'Hello'
2021-01-24 10:38:24.545628|> 'Hello'
2021-01-24 10:38:25.550777|> 'Hello'

04. Getting more information

To include file name, line number, and function information, enable includeContext:

from icecream import ic

def plus_five(num):
    return num + 5

ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))

Output:

ic| ice_test.py:7 in <module>- plus_five(4): 9
ic| ice_test.py:8 in <module>- plus_five(5): 10

05. Removing Icecream code

After debugging, you can replace ic() calls with regular print() or remove them entirely, keeping the production code clean.

from icecream import ic

def plus_five(num):
    return num + 5

ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))

for i in range(10):
    print(f'****** Training model {i} ******')

Output demonstrates that debugging statements are easy to locate and delete.

Summary

Icecream provides a simple way to produce informative debugging output in Python, showing variable values, execution context, and allowing custom prefixes, while keeping the codebase tidy after debugging.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingDevelopmenttoolscodeprinticecream
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.