Debugging with ice cream in Python
Last Updated :
26 Oct, 2022
Do you often use Python print() to debug your code? However, if you want to make a significant improvement, you could use IceCream which would make debugging faster, cleaner, and easier to read. ic(), which is short for IceCream prints both expressions/variable names and their values. ic() is faster to type than print(). The output is highlighted and printed in a structured format. If you want, you can include program context (filename, line number, and parent function) easily. You could effortlessly debug while solving competitive code problems and save a lot of time while debugging.
Installation
To install the IceCream module, you need to open up your terminal or command prompt and type the following command:
$ pip install icecream
After installing the IceCream module, we are ready to use IceCream to debug Python code efficiently.
Why use IceCream to debug
Let's look at an example where the average of three values is printed by the Python code.
Note: This is just an example, so just concentrate where and for which variable the print() function is used rather than understanding the code.
Python3
# Python program to print average of three values
a = 3
b = 11
c = 42
for i in range(80, 83):
a = a*i
b = b*i
c = c*i
ans = (a+b+c)/3
print(a)
print(b)
print(c)
print(int(ans))
Output:
$ python gfg.py
240
880
3360
1493
19440
71280
272160
120960
1594080
5844960
22317120
9918720
When we print the values of a, b, c and ans for debugging purposes, it is difficult to identify which variable corresponds to which output. But what if there are hundreds of values in the output? IceCream is very helpful in these situations for debugging.
Below is the code for printing a, b, c, and ans using IceCream ic().
Note: This is just an example, so just concentrate where and for which variable the ic() function is used rather than understanding the code.
Python program to print average of three values using ic()
Python3
from icecream import ic
a = 3
b = 11
c = 42
for i in range(80,83):
a = a*i
b = b*i
c = c*i
ans = (ic(a)+ic(b)+ic(c))/3
ic(int(ans))
Output:
$ python gfg.py
ic| a: 240
ic| b: 880
ic| c: 3360
ic| int(ans): 1493
ic| a: 19440
ic| b: 71280
ic| c: 272160
ic| int(ans): 120960
ic| a: 1594080
ic| b: 5844960
ic| c: 22317120
ic| int(ans): 9918720
Explanation: Here, ic() is used between the code to print out the values in a structured and simpler method. We are easily able to identify the values of a, b, c and ans in the output.
Simplest Example
Here's a basic example of how to use the IceCream ic() function. Any data type and data structure that python print() supports can be used with IceCream ic().
Python3
# Python program to print different
# types of datatypes
# and data structures using ic()
from icecream import ic
NUMBER = 2008
FLOAT = 95.47
BOOLEAN = True
STRING = "GFG"
LIST = [1, 2, 3, 4, 5]
DICT = {"gfg": "geeksforgeeks", "founder": "Sandeep Jain", }
ic(123)
ic(NUMBER)
ic(FLOAT)
ic(BOOLEAN)
ic(STRING)
ic(LIST)
ic(DICT)
Output
ic| 123
ic| NUMBER: 2008
ic| FLOAT: 95.47
ic| BOOLEAN: True
ic| STRING: 'GFG'
ic| LIST: [1, 2, 3, 4, 5]
ic| DICT: {'founder': 'Sandeep Jain', 'gfg': 'geeksforgeeks'}
Invoking Python Function using icecream module ic()
Here's an example to use IceCream with the python function. The implementation of ic() for printing the return value of the function is the same as using the python print function.
Python3
# Python program to implement ic()
# on functions
from icecream import ic
def square(i):
return i*i
ic(square(4))
Output:
$ python gfg.py
ic| square(4): 16
Explanation: Here, ic() prints the function with the argument that is being passed and the return value on the right.
Multiple ways to print ic() in a function
Here are a few different approaches to include ic() in a function to get basically the same result.
Python3
# Python program to understand different
# ways to print using ic()
from icecream import ic
def multiply1(i, j, k):
ic(i)
ic(j)
ic(k)
return i*j*k
def multiply2(i, j, k):
ic(i, j, k)
return i*j*k
def multiply3(i, j, k):
return ic(i)*ic(j)*ic(k)
ic(multiply1(1, 2, 3))
ic(multiply2(1, 2, 3))
ic(multiply3(1, 2, 3))
Output:
$ python gfg.py
ic| i: 1
ic| j: 2
ic| k: 3
ic| multiply1(1, 2, 3): 6
ic| i: 1, j: 2, k: 3
ic| multiply2(1, 2, 3): 6
ic| i: 1
ic| j: 2
ic| k: 3
ic| multiply3(1, 2, 3): 6
ic() helps in identifying the block of code that was executed
To determine which block of code was executed, you could use ic() to print the line that was executed. If no arguments are passed in ic(), it will print the line of code that was executed with the parent function, filename and time.
Python3
# Python program to print parent
# function, filename,
# and the line number as prefix
from icecream import ic
def gfg(value):
ic()
if value == True:
ic()
return "GFG"
else:
ic()
return -1
ic(gfg(True))
Output:
$ python gfg.py
ic| new.py:6 in gfg() at 18:42:14.916
ic| new.py:8 in gfg() at 18:42:14.917
ic| gfg(True): 'GFG'
Explanation: In the output, Line 1, 6 in gfg() indicates that the program executes the gfg() function and encounters ic() on line 6. Line 2, 8 in gfg() indicates that the program executes the if block and not the else block, as the argument value is passed 'True' and encounters ic() on the 8th line of the code. It also prints the time beside it.
ic() default configuration to print the filename, line number, and time as a prefix is false when an argument is passed. But it can be enabled by configuring the output, which we will learn next.
Using includeContext of icecream module
By configuring output as includeContext = True, the output will also print the parent function, filename, and the line number of the ic() which is executed.
Python3
# Python program to configure
# prefix as parent function,
# filename, and the line number
from icecream import ic
ic.configureOutput(includeContext=True)
def square(i):
return ic(i)*i
NUMBER = 2008
ic(NUMBER)
ic(square(23))
Output:
$ python gfg.py"
ic| 9.py:11 in <module>- NUMBER: 2008
ic| 9.py:7 in square()- i: 23
ic| 9.py:12 in <module>- square(23): 529
Enable/Disable icecream module
With ic.disable() and ic.enable(), respectively, the output of ic() can be completely turned off and then turned back on.
Python3
# Python program to enable/disable ic()
from icecream import ic
ic.disable()
ic(1990)
ic("Hello World")
ic.enable()
ic("GFG")
ic(2008)
Output:
$ python gfg.py
ic| 'GFG'
ic| 2008
Note: Here lines ic(1990) and ic("Hello World") are not getting printed.
Using configureOutput parameter in icecream module
IceCream module also enables you to configure a custom prefix. In the below example, we are going to learn how to configure the output prefix as time using a function.
Python3
# Python program to configure
# output prefix as time
from datetime import datetime
from icecream import ic
import time
ic("gfg")
ic.configureOutput(prefix="GFG ")
ic("gfg")
def time_format():
now = datetime.now()
return f'{now.strftime("%H:%M:%S")} --> '
ic.configureOutput(prefix=time_format)
for i in range(1, 6):
time.sleep(1)
ic(i)
Output:
$ python gfg.py
ic| 'gfg'
GFG 'gfg'
02:02:41 --> i: 1
02:02:42 --> i: 2
02:02:43 --> i: 3
02:02:44 --> i: 4
02:02:45 --> i: 5
Explanation: In the output, line 1 shows the default output. Line 2 has a prefix 'GFG' which was configured in the code. From the 3rd line onwards, we were able to configure output with prefix time. The time shows a delay of 1 second as time.sleep() delays it as shown in the code. Finally, remove all the ic() after the debugging is complete.
Similar Reads
Debugging in Python with Pdb
The PDB module in Python gives us gigantic highlights for compelling debugging of Python code. This incorporates: Â Pausing of the programLooking at the execution of each line of codeChecking the values of variables This module is already installed with installing of python. So, we only need to impo
6 min read
Hangman Game with a GUI in Python
In this article, we'll be building the Hangman Game. To create the game's graphical user interface (GUI), we'll be using the Pygame library in Python. Pygame is a library commonly used for making graphical user interface games. So, let's dive into creating our Hangman Game with the help of Pygame! W
8 min read
Debugging decorators in Python
Decorators in Python are really a very powerful feature. If you are a web developer and you have used the Django framework or even some other development frameworks you would have already come across decorators. For an overview decorators are wrapper functions that wrap an existing function or a met
3 min read
Creating Your Own Python IDE in Python
In this article, we are able to embark on an adventure to create your personal Python Integrated Development Environment (IDE) the usage of Python itself, with the assistance of the PyQt library. What is Python IDE?Python IDEs provide a characteristic-rich environment for coding, debugging, and goin
3 min read
Driving Headless Chrome with Python
In this article, we are going to see how to drive headless chrome with Python. Headless Chrome is just a regular Chrome but without User Interface(UI).  We need Chrome to be headless because UI entails CPU and RAM overheads. For this, we will use ChromeDriver, Which is a web server that provides us
3 min read
What is a Python egg?
Python eggs were like special containers for Python software, making it easier to share and install packages. They came about to tackle issues with an older format called "eggs," which stood for Extensible Gateway Interface. In this article, we will see what is Python egg and how we use it. What is
3 min read
Python | Basic Program Crash Debugging
If the program is crashing with an exception, running the program as python3 -i can be a useful tool for simply looking around. The -i option starts an interactive shell as soon as a program terminates. From there, the environment can be explored. Code #1: Given the code Python3 1== # abc.py def fun
2 min read
Python Debugger â Python pdb
Debugging in Python is facilitated by pdb module (python debugger) which comes built-in to the Python standard library. It is actually defined as the class Pdb which internally makes use of bdb(basic debugger functions) and cmd (support for line-oriented command interpreters) modules. The major adva
5 min read
Top 5 Easter Eggs in Python
Python is really an interesting language with very good documentation. In this article, we will go through some fun stuff that isn't documented and so considered as Easter eggs of Python. 1. Hello World Most of the programmers should have started your programming journey from printing "Hello World!
3 min read
Using CX_Freeze in Python
We have created many interesting projects and programs in Python.There may come a time when we've created something very exciting, and we want to share it. Usually, in order to share our Python program, the recipient is going to need to have the same version of Python installed, along with all of th
3 min read