Search code examples
pythondebuggingipythonbreakpoints

Debug Python Script Using Multiple Breakpoints in IPython


How do we set multiple breakpoints in Python using IPython, so that for example we stop at line 4 and line 100?

Also, can we change our mind and set extra breakpoints while already in debugging?


Solution

  • iPython provides IPython.core.debugger.Tracer debugger class which can be used to debugging.

    Let's say I have below code written in myScript.py:

    from IPython.core.debugger import Tracer
    zz = Tracer()
    
    print "iPython"
    zz()
    print "is a"
    print "command shell"
    zz()
    print "for"
    print "interactive computing"
    print "in"
    print "multiple programming languages"
    print "including"
    print "Python"
    

    As you can see I have set 2 breakpoints in the script from the beginning at Line 5 and 8. Below, I am running this script and will be setting 2 more breakpoints at line 12 & 13.

    $ ipython myScript.py
    iPython
    > /home/user/Documents/myScript.py(6)<module>()
          4 print "iPython"
          5 zz()
    ----> 6 print "is a"
          7 print "command shell"
          8 zz()
    
    ipdb> break 12
    Breakpoint 1 at /home/user/Documents/myScript.py:12
    ipdb> break 13
    Breakpoint 2 at /home/user/Documents/myScript.py:13
    

    Also, when inside debugging, you can use commands c for continue and n for next step. Hopefully, it helps you.