IceCream
Never use print() to debug again: inspect variables, expressions, and program execution with a single, simple function call. IceCream is a Python library that makes debugging effortless and more readable. It's currently at version 2.2.0 and has an active release cadence with frequent minor updates.
Warnings
- breaking Support for Python 3.8 was officially removed in IceCream v2.1.9. Prior to this, support for all Python versions before 3.8 (including Python 2) was dropped in v2.1.4.
- gotcha Calling `ic.configureOutput()` without any arguments will raise a `TypeError` as of v2.1.3. This method expects at least one configuration parameter.
- gotcha For improved debugging in IDEs like VS Code, configure IceCream to output absolute file paths, making them clickable links. By default, `contextAbsPath` is `False`.
- gotcha When dealing with complex objects like SymPy objects, earlier versions (before v2.1.6) could encounter `TypeError` during pretty-printing if dictionary keys were unorderable. This was fixed by implementing a fallback.
- gotcha To avoid leaving debugging output in production, IceCream provides `ic.disable()` to globally silence all `ic()` calls without removing them from your code. This is a common and recommended practice.
Install
-
pip install icecream
Imports
- ic
from icecream import ic
Quickstart
from icecream import ic
def calculate_sum(a, b):
result = a + b
ic(a, b, result) # Inspect multiple variables
return result
my_num1 = 5
my_num2 = 10
ic(my_num1, my_num2) # Inspect individual variables
final_sum = calculate_sum(my_num1, my_num2)
ic(f"The final sum is: {final_sum}") # Inspect expressions and strings
class MyClass:
def __init__(self, name):
self.name = name
ic(self.name)
obj = MyClass("IceCreamExample")
ic.disable() # Disable all ic() output
ic("This line will not be printed.")
ic.enable() # Re-enable ic() output
ic("This line will be printed again.")