14

Stop Using Print to Debug in Python. Use Icecream Instead

 3 years ago
source link: https://towardsdatascience.com/stop-using-print-to-debug-in-python-use-icecream-instead-79e17b963fcc
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Stop Using Print to Debug in Python. Use Icecream Instead

Are you Using Print or Log to Debug your Code? Use Icecream instead.

Image for post
Image for post
Photo by Kenta Kikuchi on Unsplash

Motivation

If you are using print to debug your code, you might find it confusing to look at many lines of output on your terminal and then try to figure out which code each output belongs to.

For example, running the script below

will give you

30
40

Which one of these outputs is num1 ? Which one of these outputs is num2 ? Two outputs might not be so bad to figure out, but what if there are more than 5 different outputs? To try to find the source code that is responsible for the output can be time-consuming.

You might try to add text to the print statement to make it easier to figure out:

num1 30
num2 40

The output is easier to read, but again, it is time-consuming to write out the text. Is there a way to print the code that is responsible for the output without additional text like below?

ic| num1: 30
ic| num2: 40

That is when icecream comes in handy.

What is icecream?

Icecream is a Python library that makes print debugging more readable with minimal code.

To install icecream, use

$ pip install icecream

Let’s try it out by print the output of a Python function.

Image for post
Image for post

By using ic , we do not only see the output but also see the function and its arguments! How convenient! The color in your terminal will also be as colorful as the outputs shown above.

Inspect Execution

To locate where the code was executed, you might do something like what is shown below to find which statement was executed

I'm user

Icecream makes it easier for you to do something like the above by simply running ic() without additional text

Image for post
Image for post

Now you know that the code at line 5, which is in function hello , was executed while the code at line 7 was not.

Custom Prefix

If you would like to insert a custom prefix such as the time the code was executed to your print statement, icecream also allows you to do so.

Image for post
Image for post

Now the time that the code is executed is automatically shown in the output! How cool is that?

Can I get more context?

Besides knowing the code that is responsible for the output, you might also want to know which line and file the code was executed from. To know the context of the code, add includeContext=True to ic.configureOutput()

Image for post
Image for post

Now you know that the first output was executed by the function plus_five from the file icecream_example.py at line 7.

Delete All Icecream after Finish Debugging

You can use icecream solely for debugging while using print for other purposes such as pretty printing

Image for post
Image for post

Since you can distinguish between debugging print and pretty print, it is much easier for you to search and delete all the ic statements after debugging.

Image for post
Image for post

After deleting all debugging prints, your code is clean!

Conclusion

Congratulations! You have just learned how to make print debugging more readable using icecream. Icecream has been a great debugging tool for me, and I hope you will find it useful as well.

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK