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
🌐
Real Python
realpython.com › python-debugging-pdb
Python Debugging With Pdb – Real Python
May 19, 2023 - If you pass a Python expression as the 2nd argument, pdb will break when the expression evaluates to true. We’ll do this in an example below. In this example, there’s a utility module util.py. Let’s set a breakpoint to stop execution in the function get_path(). Here’s the source for the main script example4.py: ... #!/usr/bin/env python3 import util filename = __file__ import pdb; pdb.set_trace() filename_path = util.get_path(filename) print(f'path = {filename_path}')
🌐
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.
🌐
Plone
5.docs.plone.org › develop › debugging › pdb.html
Python Debugging — Plone Documentation v5.2
If you wish to play around with Zope in interactive Python shell or run scripts instead of debugging (exceptions), please read Command line documentation. # Go to your code and insert the statement import pdb; pdb.set_trace() at the point where you want have a closer look.
🌐
Maurcz
maurcz.github.io › posts › 002-customizing-the-python-debugger
Customizing the Python Debugger | Mauricio R. Cruz
October 4, 2020 - I did some digging and in this ... 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....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-debugger-python-pdb
Python Debugger – Python pdb - GeeksforGeeks
November 4, 2022 - The major advantage of pdb is it runs purely in the command line, thereby making it great for debugging code on remote servers when we don't have the privilege of a GUI-based debugger. ... To start debugging within the program just insert import pdb, pdb.set_trace() commands.
Find elsewhere
🌐
Real Python
realpython.com › videos › getting-started-pdb
Getting Started With pdb (Video) – Real Python
Breaking into the debugger can be achieved with just a single line of Python code. It’s import pdb; pdb.set_trace(). When execution reaches this point in the program, the program stops and you’re dropped into the pdb debugger. Effectively, this is…
Published   September 18, 2019
🌐
HackerOne
pullrequest.com › blog › introduction-to-the-python-debugger-pdb-
Introduction to the Python Debugger (pdb) | HackerOne
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().
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › pdb.html
26.2. pdb — The Python Debugger — Stackless-Python 2.7.15 documentation
Pdb is the debugger class. The completekey, stdin and stdout arguments are passed to the underlying cmd.Cmd class; see the description there. The skip argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. [1] Example call to enable tracing with skip: import pdb; pdb.Pdb(skip=['django.*']).set_trace() New in version 2.7: The skip argument.
🌐
GitHub
github.com › spiside › pdb-tutorial
GitHub - spiside/pdb-tutorial: A simple tutorial about effectively using pdb · GitHub
The debugger is included in python's standard library and we use it the same way we would with any python library. First, we have to import the pdb module and then call one of its methods to add a debugging breakpoint in the program.
Starred by 902 users
Forked by 107 users
Languages   Python
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-the-python-debugger
How To Use the Python Debugger | DigitalOcean
August 20, 2021 - You can trigger a debugging session by importing the pdb module and adding the pdb function pdb.set_trace() above the line where you would like the session to begin.
🌐
PyPI
pypi.org › project › pdbpp
pdbpp · PyPI
(Pdb) list 1 2 def fn(): 3 c = 42 4 import pdb;pdb.set_trace() 5 -> return c (Pdb) c
      » pip install pdbpp
    
Published   Feb 23, 2026
Version   0.12.1
🌐
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.
🌐
Readthedocs
pradyunsg-cpython-lutra-testing.readthedocs.io › en › latest › library › pdb.html
pdb — The Python Debugger - Python 3.12.0a0 documentation
Source code: Lib/pdb.py The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, i...
🌐
Avogadro
avogadro.cc › docs › building-molecules › importing-from-the-pdb
Importing from the Protein Data Bank (PDB) - Avogadro
May 23, 2022 - Older versions of Avogadro have a bug with direct access to the PDB (since the website has moved) but using v 1.2.0, you can again use File > Import > Fetch from PDB
🌐
Index.dev
index.dev › blog › debug-python-scripts-with-pdb
How to Debug Python Scripts Using PDB: A Debugger Tutorial
March 20, 2025 - Debug Python scripts using PDB by adding breakpoints with import pdb; pdb.set_trace(), stepping through code, inspecting variables, and identifying issues to fix bugs efficiently.