Ir al contenido principal

Stop python debugger exactly on the failing loop iteration

This post is for ipdb users.

Is this case familiar for you?:
  • Write a break point on your code
  • Go into a loop
  • Start pressing 'c' and 'n' until you found the iteration that is failing.
It can works, but you can only do this ... one or two times maybe??
If you need to this frequently, this method doesn't works and you need another tool, but .... what's the tool I need?

You need launch_ipdb_on_exception


...
...

from ipdb import launch_ipdb_on_exception

with launch_ipdb_on_exception():
    for i in range(9):
        print(i)
        if i==4:
            raise Exception('Debug time!')

... # Output
0
1
2
3
4
Exception('Debug time!',)
> <ipython-input-1-4f44dca149ad>(7)<module>()
      5         print(i)
      6         if i==4:
----> 7             raise Exception('Debug time!')

ipdb> i  # Note ipdb console!!
4
ipdb> c  # Continues the execution

In [2]: i  # Ipython console again
Out[2]: 4

In [3]: 

Doing this your code will stop exactly on the iteration that's failing.

Comentarios