Debugging 101: Replace print() with icecream ic()

351,585
333
Published 2023-10-05
Today we learn about the Python package called icecream, which you should use instead of print when debugging your code.

◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine.com/books/
💻 The Algorithm Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop

💼 Services 💼
💻 Freelancing & Tutoring: www.neuralnine.com/services

🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: www.instagram.com/neuralnine
🐦 Twitter: twitter.com/neuralnine
🤵 LinkedIn: www.linkedin.com/company/neuralnine/
📁 GitHub: github.com/NeuralNine
🎙 Discord: discord.gg/JU4xr8U3dm

All Comments (21)
  • @monkmcfunk
    You could do this all much quicker by just inserting the built in python debugger with the line "import pdb; pdb.set_trace()" where you would normally put your print statement. It will then start the debugger in your terminal and you can view and even change all variables during execution. It also helps finding exactly where your code is breaking as you're stepping through the code line by line. I can't recommend it enough.
  • @davidrusca2
    You know, from 3.10 onward you can essentially do this without packages by doing print(f"{variable=}"). Putting an = after the thing you are formating makes it so that it tells you the name/calculation together with the value.
  • @thetruth45678
    One of the first things I do in any new language is create a debugging system. Helps to get you confortable with the syntax and environment, and gives you the tools up front to progress more efficiently. This is nice, too.
  • @doofsCat
    Learnt something useful today. Thanks, this is something I will probably use in my classes as debugging is what I do 99% of the time.
  • @RobertLugg
    Very useful. Don't worry about "professional" way. Log files and print statements are the way to debug large runtime programs.
  • @benlpayne
    I would suggest skipping ice cream and going straight to python logging. It give much more control over how/what logging in emitted. I have found on many projects that starting from day 1 with using logging, instead of print is a great idea. The only thing I see that ic does that other don't is the quick ability to wrap a lines of code in ic and get a decent print that can help quickly debug issues. That said a debugger could also solve that use case.
  • @BillyChan77
    Not going to lie, I use A LOT of print statement to debug. This is very cool , learn something new and will start trying TODAY!
  • @Syfmuna
    Thanks so much, I'm constantly learning new things from your videos and it's like this video was made for me. I was feeling specifically targeted when you were talking about using print(a) and print(type(a)), etc. 😄
  • @vani8109
    Only after hearing it a dozen times did I realize that the function ic() is supposed to be a pun "I see" lol
  • @TheRealChicken
    5:22 Why the hell does the print execute before the ic? or is the ic running in like a thread seperate from the main thread and just takes longer?
  • @serosgb
    🎯 Key Takeaways for quick navigation: 00:00 📽️ Introduction to Ice Cream - Ice cream is introduced as a Python package for debugging and logging. 00:19 🐍 The Need for Debugging in Python - Discusses common debugging challenges in Python, where code doesn't behave as expected. - Explains the use of print statements for debugging. 02:29 🚀 Installing and Importing Ice Cream - Demonstrates how to install and import the Ice Cream (IC) package for Python debugging. 02:54 ➕ Simple Debugging with IC - Shows how to use Ice Cream to debug a basic addition function, providing a convenient way to display function calls and results. 06:06 📃 Improved Printing of Data Structures - Highlights the enhanced output format when using Ice Cream to print dictionaries, making it easier to understand complex data structures. 07:30 📍 Inserting IC Anywhere in the Code - Explains how Ice Cream can be placed anywhere in the code, helping to track which parts of a function are being executed. 09:02 ⚙️ Configuring Ice Cream - Demonstrates how to configure Ice Cream with custom prefixes and output functions, and how to enable or disable it for specific sections of code. 11:00 📝 Logging Output with IC - Shows how Ice Cream can be configured to log output to a file, making it useful for professional debugging and documentation. Made with H
  • @davidfell5496
    Interesting. Will check it out. The 'timing' of when the icecream output happens looks relevant and you didn't touch on the behaviour there. But a nice introduction. Thank you.
  • @CodePhiles
    this is good to take debugging step by step, it reaches out to be complicated at the end, so I think it would be better to show the real level at the end, which is the built in debugger, where you can do all the debugging techniques ( breakpoints, tracing code line by line, viewing all variables..etc )
  • @MegaJohn144
    I just found out about ice cream a couple of days ago. It was just what I needed when I needed it. Right now, I am developing in Flask, which doesn't print to the console, unless you flush. Ice cream takes care on that.
  • @MrFlexNC
    People will move mountains so they don't have to use the logging package
  • @Omnifarious0
    I don't think there's anything more 'professional' about using a debugger. And I'm really happy to learn about this new tool. It looks awesome. I sort of wonder if the enable and/or disable functionality can be used as a context manager.
  • This is a nice showcase of icecream IC. Looks nice for some smaller projects. I do think, however, that there are better options to replace a good old print() statement, some of which are named in other comments. As for the pretty printing, if you add a bit more depth to some of those dictionarie, it could become bothersome to find what you are looking for. It is nice to be able to write the output to a file. Alternative for this includes the logging module which allows you to log on certain levels. Using logging together with some of the other builtin features might give you a nice solution that depends on standard library only. It is convenient, however, to have everything with a simple function call.
  • @Kirizan
    I noticed at around 8:30, you have the ic() statement before the return value, but in the console output, the return value comes before the ic() output. Is there a reason for this?
  • @mr14hsoj
    Personally, I think f-strings are almost equivalent and prevent me from adding another dependency to my project. For example, print(f'{add(30, 70) = }') will result in printing: add(30, 70) = 100 This is essentially the same thing that printing with ic will do, just using an '=' instead of a ':'. I think the nice dictionary printing is a good feature, but I usually just use pretty print (pprint) for this. E.g. import pprint pprint.pprint(data) This does still need to be imported, but only if you need to be printing dictionaries this way AND it's a built-in module so still no extra dependency.
  • While I can appreciate the tool (I'd written myself helpers that do similar things, so good to know there's a fully developed, feature-rich package for it), I feel that yous examples here made me go "akshually, that's what proper unit tests should do".