Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable pdb.set_trace() without stopping python program and edit the code

Tags:

python

pdb

I suspect that I have issue in one of my loops, so I setup a break points with pdb.set_trace()

import pdb
for i in range(100):
    print("a")
    pdb.set_trace()
    print("b")

after check variable in this loop for a few times, I decide continue this programming without further breaks. So I try to get the break number with b command, no breaks listed. I guess this line of code don't setup a break point. but How Do I get ride of this "break points" without stopping the program and change the code?

like image 327
scott huang Avatar asked Sep 12 '17 01:09

scott huang


1 Answers

Setting a breakpoint (requires Python 3.7):

breakpoint()

Disabling breakpoints set with the breakpoint() function:

import os
os.environ["PYTHONBREAKPOINT"] = "0"

Long story:

In the 3.7 version of Python, the breakpoint() built-in function for setting breakpoints was introduced. By default, it will call pdb.set_trace(). Also, since the 3.7 version of Python the PYTHONBREAKPOINT environment variable is available. It is considered when the breakpoint() function is used.
So, in order to disable these breakpoints (set with the breakpoint() function), one can just set the PYTHONBREAKPOINT environment variable like this:

import os
os.environ["PYTHONBREAKPOINT"] = "0"

It may be useful to mention here sys.breakpointhook() which was also added in the 3.7 version of Python and allows to customize breakpoints behavior.

like image 71
Pavel Shishpor Avatar answered Oct 04 '22 07:10

Pavel Shishpor