Live data from Hacker News

Icecream: Never use print() to debug again in Python

github.com

81–90 of 271 posts

Re: Icecream: Never use print() to debug again in Python

#82

I promise the 15 minutes it takes to learn to use the debugger will save years of your life

Which debugger would you recommend?

For Python: ipdb. In your code, you can just drop in "import ipdb; ipdb.set_trace()" and the execution will stop there with an interactive prompt. Alternatively start the script through (i)pdb and set breakpoints.

You can also use plain pdb ("import pdb; pdb.set_trace()"), which has the advantage that it comes with the Python stdlib, but the interactive prompt is less fancy (no history, no autocompletion, etc).

Re: Icecream: Never use print() to debug again in Python

#83
post #60

I am always going to use print to debug in every programming language I can until the day I die.

Absolutely, I don't understand why using print() or its equivalent in other languages is looked down upon. It's quick way to narrow down the "area of search" before bringing in the big guns.

> It's quick way to narrow down the "area of search" before bringing in the big guns.

What are the big guns? with a debugger, I can stick a breakpoint and look at the entire state of everything. Given we're talking about Python, in pycharm [0] you can even execute your print statements in the debugger if you so wish. If you get the location wrong, or want to see what's going on elsewhere you can just continue execution and use another breakpoint.

This is even more important if you have a long compile/deploy cycle (I work in games, and rebuilding and deploying to a console can be a >10 minute iteration time)

[0] https://www.jetbrains.com/help/pycharm/part-1-debugging-pyth...

Re: Icecream: Never use print() to debug again in Python

#84
post #81

Humm, I built a similar but more powerful tool: https://github.com/samuelcolvin/python-devtools (install with `pip install devtools`) It has similar functionality for debugging but has prettier formatting and code highlighting using pygments.

One of the most useful trick I found was installing the tool via sitecustomize.py so you don't need the import:

    # add devtools debug to builtins
    try:
        from devtools import debug
    except ImportError:
        pass
    else:
        __builtins__['debug'] = debug
(see https://github.com/samuelcolvin/python-devtools#usage-withou...)

This would work with icecream too.

The second advantage of not needing the import is that CI fails if you forget to remove all debug() commands.

Re: Icecream: Never use print() to debug again in Python

#85
post #76

Earlier quoted context omitted.

In the docs you can look up breakpoint, it has a lot of features amongst other things you can register a custom handler. I use it in selenium tests so I can either debug on error or just print the error message and continue

Could you provide a concrete example? It's still unclear to me why one wants a custom breakpoint() handler.

You can run Python with

    PYTHONBREAKPOINT=print python
and all your breakpoint() calls will be prints.

Which is not super useful, however you can use

    PYTHONBREAKPOINT=icecream.ic
and now you get the advantages of TFA without having to import anything anywhere.

The only annoyance is that the default hook takes no arguments, so you can't trivially switch between the default and custom implementations, you may need to modify the source depending on the breakpoint hook you're using.

Re: Icecream: Never use print() to debug again in Python

#87
Instead of introducing a new library, why not just instruct your editor to do the heavy lifting for you? For instance, I've written a small Emacs function that asks for the argument to print in the Minibuffer and then inserts it once quoted, once unquoted.

It's quite nice to have such a function for any programming language you use and bind it to the same keyboard short-cut.

Re: Icecream: Never use print() to debug again in Python

#88

You can do the same thing with Python 3.8+ by using f-strings and just appending "=" to the variable name: >>> print(f"{d['key'][1]=}") d['key'][1]='one'

You can also make it a bit prettier by adding spaces:

  >>> print(f"{d['key'][1] = }")
  d['key'][1] = 'one'
It works with any expression you like, not just variables:

  >>> print(f'{np.sin(np.pi/4.) = }')
  np.sin(np.pi/4.) = 0.7071067811865475

Re: Icecream: Never use print() to debug again in Python

#90
post #87

Instead of introducing a new library, why not just instruct your editor to do the heavy lifting for you? For instance, I've written a small Emacs function that asks for the argument to print in the Minibuffer and then inserts it once quoted, once unquoted. It's quite nice to have such a function for any programming language you use and bind it to the same keyboard short-cut.

For one, people not using emacs, or whatever editor it would be, can use it.
Post reply on HN