Is it preferred to use the second approach?
In my opinion: Yes.
As you said, no need to include the pdb module in general.
Therefore import pdb; pdb.set_trace() is a convenient way to include it when debugging, so that easily can be removed when debugging is done. This is the way I always do it, and probably lots of other people do it like that too.
python - Difference between import pdb at the beginning and import pdb; pdb.set_trace() - Stack Overflow
how to disable pdb.set_trace() without stopping python program and edit the code - Stack Overflow
Python pdb.set_trace() does not work as expected in Google Colab
Simpler way to put PDB breakpoints in Python code? - Stack Overflow
Videos
to my knowledge, you could not bypass set_trace, but you could neutralize it, once debugger stopped, type:
pdb.set_trace = lambda: 1
then continue, it wont break again.
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.
You can run your program into pdb from the command line by running
python -m pdb your_script.py
It will break on the 1st line, then you'll be able to add a breakpoint wherever you want in your code using the break command, its syntax is:
b(reak) [[filename:]lineno | function[, condition]]
It is flexible enough to give you the ability to add a breakpoint anywhere.
You can use:
from pdb import set_trace as bp
code
code
bp()
code
code
Hi,
This is the problem, I have this:
@.mark.django_db def test_it(): .... # some code a= '' import pdb; pdb.set_trace() b='' .... # some code
But It stops in:
> /usr/local/lib/python3.7/site-packages/pytz/__init__.py(230)utcoffset()
-> def utcoffset(self, dt):
(Pdb) l
225 def fromutc(self, dt):
226 if dt.tzinfo is None:
227 return self.localize(dt)
228 return super(utc.__class__, self).fromutc(dt)
229
230 -> def utcoffset(self, dt):
231 return ZERO
232
233 def tzname(self, dt):
And I don't know why. Any idea?