Print () is a built-in function that most people learning Python use to debug code.

So in a large project, if you also use print to debug your Python code, you’ll find that your terminal has multiple outputs.

Then you have to figure out what the output of each line of code is.

For example, run the following program.

num1 = 30
num2 = 40 

print(num1)
print(num2)
Copy the code

Output the result.

30 to 40Copy the code

Which of these outputs is NUM1? Which one is num2?

Finding two outputs might not be too difficult, but what if there are more than five different outputs? Trying to find the code associated with the output can be time-consuming.

Of course you can add text to the print statement to make it easier to understand:

num1 = 30
num2 = 40 

print("num1" num1)
print("num2" num1)
Copy the code

Output the result.

num1 30
num2 40
Copy the code

This result is easy to understand, but it takes time to write the relevant information.

This is where “Icecream” comes in

01. What is Icecream?

Icecream is a Python third-party library that makes print debugging clearer with minimal code.

Install the Icecream library using PIP.

pip install icecream
Copy the code

Let’s try this out by printing the output of a Python function.

from icecream import ic 

def plus_five(num):
    return num + 5

ic(plus_five(4))
ic(plus_five(5))
Copy the code

The output is as follows.

ic| plus_five(4): 9
ic| plus_five(5): 10
Copy the code

By using icecream, we can see not only the output of the function, but also the function and its parameters!

02. Check the execution

If you want to find out where the code was executed, you can find out which statement was executed by doing something like this.

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

hello(user=True)
Copy the code

Output the result.

I'm user
Copy the code

With ICecream, you can easily do this without extra text information.

from icecream import ic 

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

hello(user=True)
Copy the code

The output is as follows.

IC | ice_1. Py: 5 in the hello () at 02:34:41. 391Copy the code

From the output, line 5 of the hello function is executed, but line 7 is not.

03. Customize prefixes

If you want to insert custom prefixes (such as code execution time) into print statements, icecream can do that, too.

from datetime import datetime
from icecream import ic 
import time
from datetime import datetime

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

ic.configureOutput(prefix=time_format)

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

The output is as follows

10:38:23 2021-01-24. 509304 | > 'Hello,' the 2021-01-24 10:38:24. 545628 | > 'Hello,' the 2021-01-24 10:38:25. 550777 | > 'Hello'Copy the code

You can see the execution time of the code, displayed just before the output.

Get more information

In addition to knowing the code associated with the output, you may also want to know the lines and code files that the code executes.

In ic.configureOutput(), set the includeecontext parameter to True.

from icecream import ic 

def plus_five(num):
    return num + 5

ic.configureOutput(includeContext=True)
ic(plus_five(4))
ic(plus_five(5))
Copy the code

The output is as follows.

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

Here we see that the first output is executed by the function plus_five on line 7 of icecream_example.py.

The second output is executed by the function plus_five on line 8 of the code file.

Both operations use the ic.configureOutput() function.

Looking at the source code, you can see that there are four parameters you can set.

  • Prefix: user-defined output prefix
  • OutputFunction, which changes the outputFunction
  • ArgToStringFunction, a custom parameter serialized string
  • IncludeContext, including file names, code lines, and function information

05. Delete Icecream code

Finally, you can use icecream just for debugging and print for other purposes (such as nice printing).

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} ******')
Copy the code

Output the result.

ic| ice_1.py:7 in <module>- plus_five(4): 9
ic| ice_1.py:8 in <module>- plus_five(5): 10
****** Training model 0 ******
****** Training model 1 ******
****** Training model 2 ******
****** Training model 3 ******
****** Training model 4 ******
****** Training model 5 ******
****** Training model 6 ******
****** Training model 7 ******
****** Training model 8 ******
****** Training model 9 ******
Copy the code

Because you can distinguish debug print from beautiful print, it is very easy to search and delete all IC debug statements.

After removing all debugging code, your Python code should be clean.

conclusion

At this point, you should have learned how to use Icecream to print debugging.