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.

Answer from Carl Winbäck on Stack Overflow
🌐
Python
docs.python.org › 3 › library › pdb.html
pdb — The Python Debugger
The user can set the default backend to use if none is specified when instantiating Pdb. If no backend is specified, the default is 'settrace'. ... breakpoint() and set_trace() will not be affected by this function.
🌐
InterWorks
interworks.com › home › get to know pdb, the python debugger
Get to Know pdb, the Python Debugger - InterWorks
January 27, 2022 - There are many different ways to use pdb, but the simplest is by using its pdb.set_trace() function. Wherever you put this line in your code, the Python interpreter will stop and present an interactive debugger command prompt. From here, you can run any arbitrary Python code, or issue pdb commands to step through or even into the code by using next and step, respectively.
Discussions

python - Difference between import pdb at the beginning and import pdb; pdb.set_trace() - Stack Overflow
I usually put the line import pdb on top of my program code, along with other imports. For debugging, I then only have to type pdb.set_trace() in the spot where I want the program to halt. The ini... More on stackoverflow.com
🌐 stackoverflow.com
how to disable pdb.set_trace() without stopping python program and edit the code - Stack Overflow
To prevent breaking into debugger every time set_trace() is encountered, a similar trick as above (changing the symbol's reference to point to a harmless function) can be applied. However, the namespace of the debuggee has to be modified, not one of the debugger itself. Simply overwriting pdb.se... More on stackoverflow.com
🌐 stackoverflow.com
Python pdb.set_trace() does not work as expected in Google Colab
I'm attempting to use Python 3.8 built-in debugger (pdb) to single-step through Python code in a Jupyter notebook on Google Colab. I'm expecting import pdb; pdb.set_trace() to stop program ... More on github.com
🌐 github.com
6
December 8, 2022
Simpler way to put PDB breakpoints in Python code? - Stack Overflow
Just a convenience question. I've been a bit spoiled with debuggers in IDEs like Visual Studio and XCode. I find it a bit clumsy to have to type import pdb; pdb.set_trace() to set a breakpoint (I'd More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stanford University
web.stanford.edu › class › physics91si › 2013 › handouts › Pdb_Commands.pdf pdf
Pdb Commands Physics 91SI, Spring 2013 Rex Garland and Gabe Ehrlich
pdb.set_trace() begin the debugger at this line when the file is run · normally · Navigating Code (within the Pdb interpreter) l(ist) list 11 lines surrounding the current line · w(here) display the file and line number of the current line · n(ext) execute the current line ·
🌐
Real Python
realpython.com › python-debugging-pdb
Python Debugging With Pdb – Real Python
May 19, 2023 - In this example, the function get_path() is called. To inspect what’s happening in this function, I’ve inserted a call to pdb.set_trace() to pause execution just before it returns:
🌐
Sunscrapers
sunscrapers.com › blog › python-debugging-guide-pdb
Debugging Python Apps: A Comprehensive Guide to pdb
September 8, 2023 - To explore Python debugging with pdb, this article introduces its features, commands, real-world applications, and third-party enhancements. ... Unravel the Power of Python with the Best! ... Discover how software, data, and AI can accelerate your growth. Let's discuss your goals and find the best solutions to help you achieve them.Contact us · Hi there, we use cookies to provide you with an amazing experience on our site. If you continue without changing the settings...
Find elsewhere
🌐
Medium
medium.com › @albertbingei_71011 › try-import-pdb-and-pdb-set-trace-to-debug-your-python-code-better-448c66c7ee01
Try import pdb and pdb.set_trace() to debug your python code better | by Albert Bingei | Medium
June 2, 2022 - Simply import pdb, then pdb.set_trace() on the line that we want to inspect, and press run if using an IDE such as PyCharm or VSCODE, or call python myprogram.py on a terminal window.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-debugger-python-pdb
Python Debugger – Python pdb - GeeksforGeeks
November 4, 2022 - To start debugging within the program just insert import pdb, pdb.set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
🌐
W3Resource
w3resource.com › python-interview › what-is-the-difference-between-pdb-dot-set_trace-and-breakpoint-in-python.php
Comparing 'pdb.set_trace()' and breakpoint() in Python
In older Python versions (Python 2.x and Python 3.6 or earlier), pdb.set_trace() is the common way to initiate interactive debugging.
🌐
pytest
docs.pytest.org › en › stable › how-to › failures.html
How to handle test failures - pytest documentation
When breakpoint() is called and PYTHONBREAKPOINT is set to the default value, pytest will use the custom internal PDB trace UI instead of the system default Pdb.
🌐
Maurcz
maurcz.github.io › posts › 002-customizing-the-python-debugger
Customizing the Python Debugger | Mauricio R. Cruz
October 4, 2020 - One of the most basic things a young python developer will learn is how to put a breakpoint in the source code. For python < 3.7, the most common way to do this is with import pdb; pdb.set_trace. Those commands will import the standard pdb module and call function set_trace(), which will throw ...
🌐
GitHub
github.com › googlecolab › colabtools › issues › 3279
Python pdb.set_trace() does not work as expected in Google Colab · Issue #3279 · googlecolab/colabtools
December 8, 2022 - Call Location: File "/usr/lib/python3.8/bdb.py", line 334, in set_trace sys.settrace(self.trace_dispatch) ... Furthermore the (Pdb) prompt (or command-line) is not very useful, because if I give it the "where" command I don't get Source-Level debug with a pointer to the next instruction.
Author   vtrandal
🌐
freeCodeCamp
freecodecamp.org › news › debugging-in-python-using-pdb
How to Debug Your Python Code with the Python Debugger (pdb)
September 27, 2022 - To break into the pdb debugger, you need to call import pdb; pdb.set_trace() inside your function.
🌐
LinkedIn
linkedin.com › pulse › debugging-python-pdb-bixly
Debugging Python With PDB
August 23, 2022 - “Where is this error coming from?? Everything looks like it should work but it just isn’t!!” We’ve all been in this situation at one point or another. Debugging code isn’t always fun but it doesn’t have to be difficult.
🌐
HackerOne
pullrequest.com › blog › introduction-to-the-python-debugger-pdb-
Introduction to the Python Debugger (pdb) | HackerOne
December 12, 2023 - To use pdb, you first need to import it into your Python script. This can be done by simply adding import pdb at the beginning of your file. Once imported, you can set breakpoints in your code by calling pdb.set_trace().
🌐
YouTube
youtube.com › watch
Python 101 - Debugging Your Code with pdb - YouTube
Learn how to debug your Python programs using Python's built-in debugger, pdb with Mike DriscollIn this tutorial, you will learn the following: - Starting pd...
Published   August 19, 2022
🌐
Reddit
reddit.com › r/learnpython › pdb trace don't stop where i put the breakpoint
r/learnpython on Reddit: pdb trace don't stop where I put the breakpoint
June 1, 2023 -

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?