w3resource

What are breakpoints in debugging, and how do you set them using pdb in Python?

Using breakpoints for effective debugging with pdb in Python

Breakpoints in debugging are specific points in the code where you want the debugger to pause its execution. This allows you to inspect the program's state and variables at that location. A breakpoint is useful for analyzing code behavior during program execution and identifying issues.

Setting breakpoints using 'pdb' (Python debugger) is a straightforward process. Here's how you can set breakpoints at specific lines in the code using 'pdb':

Import pdb:

First, import the "pdb" module at the beginning of your Python script to use the "pdb" debugger.

import pdb

Place pdb.set_trace():

Next, insert the pdb.set_trace() statement at the location in our code where you want to set the breakpoint. When the Python interpreter encounters this statement during execution, it pauses and enters the "pdb" debugger.

pdb.set_trace()

Run the Script with -m pdb:

To start the Python script with the 'pdb' debugger, run the script from the command line with the -m pdb option, followed by the script's name.

python -m pdb your_filename.py

Alternatively, we can use

python -m pdb -c 

continue test.py to start the script and automatically continue execution until the first breakpoint is reached.

Inspect Variables and Step Through Code:

When the debugger is active and at the breakpoint, we can use various "pdb" commands to inspect variables, step through the code, and analyze the program's behavior. Some commonly used commands include:

  • p variable_name: Print the value of a specific variable.
  • n: Execute the current line and stop at the next line.
  • s: Step into the function call (if applicable).
  • c: Continue execution until the next breakpoint or until the program finishes.

We can use these commands to interactively debug your code and gain insights into its behavior at different points in execution.

Here's an example of how to set a breakpoint using pdb:

Code:

# test.py
import pdb
def divide(a, b):
    result = a / b
    return result
x = 9
y = 0
# Set a breakpoint here
pdb.set_trace()
z = divide(x, y)
print(z)
Output:
runfile('C:/Users/ME/test.py', wdir='C:/Users/ME')
> c:\users\me\test.py(10)<module>()
      7 y = 0
      8 # Set a breakpoint here
      9 pdb.set_trace()
---> 10 z = divide(x, y)
     11 print(z)

Let we inspect variables and step through code using "pdb" commands.

  • Open a terminal or command prompt.
  • Navigate to the directory where your test.py script is located.
  • Run the script using the python command (without -m pdb):
  • python your_filename.py
  • The script will execute and pause at the pdb.set_trace() statement.
  • You will see a (Pdb) prompt, indicating that you are now in the pdb debugger.
  • Use the 'p' command to inspect the variables:
  • (Pdb) p x
    (Pdb) p y
    (Pdb) p z
  • To continue with the execution, type 'c' to execute the divide() function and print the value of z.

Run in the Anaconda Open terminal

(base) C:\Users\ME>python test.py
> c:\users\me\test.py(10)<module>()
-> z = divide(x, y)
(Pdb) p x
9
(Pdb) p y
0
(Pdb) n
ZeroDivisionError: division by zero
> c:\users\me\test.py(10)<module>()
-> z = divide(x, y)
(Pdb) s
--Return--
> c:\users\me\test.py(10)<module>()->None
-> z = divide(x, y)
(Pdb) c
Traceback (most recent call last):
  File "C:\Users\ME\test.py", line 10, in <module>
    z = divide(x, y)
  File "C:\Users\ME\test.py", line 4, in divide
    result = a / b
ZeroDivisionError: division by zero
(base) C:\Users\ME>

This approach should properly activate the "pdb" debugger and allow you to inspect the values of the variables x, y, and z without encountering the NameError issue.

Note: Avoid using python -m pdb your-filename.py when your script already contains a pdb.set_trace() statement. Instead, start the script directly with the python command to use the pdb debugger effectively.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-interview/what-are-breakpoints-in-debugging-and-how-do-you-set-them-using-pdb-in-python.php