1

I use ipython and run -d foo.py to debug my program. But every time I re-debug the program, I have to reset all the breakpoints. This is annoying when you have multiple breakpoints or multiple py files.

Is it possible to let ipython or pdb to remember the breakpoints and reused them in next debug session.

1 Answer 1

4

You can write an initialisation file for pdb which lists all the break points you want to add to the program. It must be called .pdbrc and placed either in the working directory or your home directory. Break points can be specified by either line number or by function name.

eg.

a.py

import b

def doX():
    print("in x") # line 4
    b.doY()

if __name__ == "__main__":
    doX()

b.py

def doY():
    print("in y") # line 2

.pdbrc

# the following are all equivalent -- placing a breakpoint on entry into doX
break 4
break a.py:4
break doX
break a.doX

# placing a breakpoint on entry into doY
break b.py:2
break b.doY

Output

In [8]: %run -d a.py
Breakpoint 1 at /home/user/Desktop/python-stuff/a.py:1
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
Breakpoint 2 at /home/user/Desktop/python-stuff/a.py:3
Breakpoint 3 at /home/user/Desktop/python-stuff/b.py:1
> /home/user/Desktop/python-stuff/a.py(1)<module>()
1---> 1 import b
      2 
2     3 def doX():

ipdb> c
> /home/user/Desktop/python-stuff/a.py(4)doX()
2     3 def doX():
----> 4         print("in x")
      5         b.doY()

ipdb> c
in x
> /home/user/Desktop/python-stuff/b.py(2)doY()
3     1 def doY():
----> 2         print("in y")
      3 

ipdb> c
in y
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Dunes ! Just want to emphasis that, if the b.py is not in current directory, but in another folder ./dir1/dir2/b.py. In the .pdbrc, we need to set bread dir1/dir2/b.doY.
Just checked, and pdb also includes the restart command which restarts the program with the same breakpoints and options. That was probably the simpler answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.