Stop Using print(): Master Python Debugging with IceCream ๐Ÿฆ

Python Debugging with IceCream 01

We all do it. You have a bug in your Python code, so you start typing:

print("here")
print(variable)
print("----------------")

It works, but it’s messy. You have to type labels manually to know which variable is which. You have to remember to delete them all later. And your terminal output looks like a disaster.

In 2026, there is a better way. Itโ€™s called IceCream.


What is IceCream?

IceCream (ic) is a tiny Python library that replaces the standard print() function for debugging. It makes your output readable, colorful, and informativeโ€”without any extra effort.

Step 1: Install it

Open your terminal (hopefully inside VS Code with your new extensions!) and run:

pip install icecream

Why It Wins: The “Old Way” vs. The “IceCream Way”

Python Debugging with IceCream 02

1. No More Manual Labels

The Old Way: You have to type the variable name twice.

x = 10
print("x:", x) 
# Output: x: 10

The IceCream Way: It knows the variable name automatically.

from icecream import ic
x = 10
ic(x)
# Output: ic| x: 10

2. It Tells You “Where” You Are

Have you ever printed something inside a loop or a function and didn’t know which function was running? IceCream tells you the filename, line number, and function name.

def complex_math(num):
    return num * 2

ic(complex_math(5))
# Output: ic| complex_math(5): 10

3. It Returns Values (Magic!) ๐Ÿช„

This is the coolest feature. print() returns None, which means you can’t use it inside a calculation. ic() returns the value, so you can wrap it around anything without breaking your code.

# This breaks your code:
# result = print(5) + 10  <-- Error!

# This works perfectly:
result = ic(5) + 10
# Output: ic| 5
# Result is 15

Advanced: Customize Your IceCream ๐Ÿจ

Once you get comfortable with the basics, you can tweak IceCream to fit your specific workflow. Here are two power moves for pros.

1. Add a Custom Prefix

By default, the prefix is ic|. But if you are debugging multiple files, you might want to make it stand out more.

ic.configureOutput(prefix='DEBUG ๐Ÿž -> ')
ic("test")
# Output: DEBUG ๐Ÿž -> 'test'

2. Write to a Log File (Instead of Terminal)

If you are running a script that takes hours, you don’t want to stare at the terminal. You can tell IceCream to write every debug line to a file automatically.

def write_to_file(text):
    with open("debug.log", "a") as f:
        f.write(text + "\n")

ic.configureOutput(outputFunction=write_to_file)

ic("This goes to the file, not the screen!")

Pro Tip: Leave it in Production (Safely)

The worst part about print() debugging is deleting all the lines before you push your code to GitHub. With IceCream, you can just disable it globally.

ic.disable()  # Turns off all debug prints
# ic.enable() # Turns them back on

Conclusion

Stop wasting time formatting your debug logs. Debugging should be fast, sweet, and simple.

Give IceCream a try this weekend. Your eyes (and your codebase) will thank you.

Ready for more automation? Next week, we are diving into local AI and showing you how to run Llama 3 on your own laptop!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top