w3resource

Exploring pdb in Python: Interactive debugging

Navigating debugging with pdb in Python

pdb is the built-in Python debugger, short for "Python Debugger." It is a powerful tool that allows developers to interactively debug their Python code by stepping through the execution line by line, inspecting variables, and identifying bugs or issues.

Using pdb for Debugging:

Starting the Debugger:

To start debugging with pdb, you need to import the pdb module and insert the pdb.set_trace() function at the location in your code where you want to start debugging. When the Python interpreter reaches this point during execution, it pauses and enter debugging mode.

Stepping through the code:

Once the debugger is active, we can use various commands to step through the code. The primary commands are:

  • 'n' (next): Execute the current line and stop at the next line.
  • 's' (step into): Execute the current line and stop at the first possible occasion (e.g., when calling a function).
  • 'c' (continue): Resume execution until the next breakpoint or until the program finishes.

Inspecting the variables:

While debugging, you can inspect variable values at any point in the code. Use the "p" command followed by the variable name to print its value. For example, 'p' variable_name.

Setting Breakpoints:

A breakpoint is a specific point in the code where you want the debugger to pause execution. You can set breakpoints at specific lines using the 'b' command followed by the line number. For example, "b 10" sets a breakpoint at line 10.

Continuing execution:

If you want to run the code until the next breakpoint or until the program finishes, use the 'c' command. This allows you to skip specific parts of the code during debugging.

Conditional Breakpoints:

You can set conditional breakpoints that pause execution only when specific conditions are met. Use the 'b' command followed by the line number and a condition in curly braces. For example, "b 20 if variable == 10".

Handling exceptions:

When an exception is raised during debugging, the debugger will pause at the point where the exception occurs. You can then inspect variables and trace the call stack to identify the exception cause.

Exiting the debugger:

To exit the debugger and continue normal program execution, use the 'q' command.

Example usage:

To use pdb effectively for debugging, it's essential to run the script interactively in the Python interpreter after entering debugger mode. You should not execute the script directly from the command prompt.

Here's how to use pdb effectively for debugging:

Code:

# test.py
import pdb
def divide(a, b):
    result = a / b
    return result

x = 10
y = 0
# Place the pdb.set_trace() statement after the variables are defined
pdb.set_trace()
z = divide(x, y)
print(z)

Run in the Anaconda Python terminal

>>> exec(open("test.py").read())
> <string>(11)<module>()
(Pdb) p x
10
(Pdb) p y
0
(Pdb) p z
*** NameError: name 'z' is not defined
(Pdb) c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 11, in <module>
  File "<string>", line 4, in divide
ZeroDivisionError: division by zero

Explanation:

Let's break down the output step by step:

  • >>> exec(open("test.py").read()): This command executes the test.py script inside the Python interpreter. The pdb.set_trace() statement inside test.py will pause execution and enter debugging mode.
  • > <string>(11)<module>(): The debugger pauses execution on line 11 of the script (which is the pdb.set_trace() statement).
  • (Pdb) p x: You inspect variable x's value, and it returns 10. This shows that x has been successfully defined and initialized with 10.
  • (Pdb) p y: You inspect variable y's value, and it returns 0. This shows that y has been successfully defined and initialized with 0.
  • (Pdb) p z: When inspecting variable z, you encounter a NameError. This is because z is not yet defined at this point. The variable z is defined and assigned a value in the next line, after the pdb.set_trace() statement.
  • (Pdb) c: You continue the script execution by typing c. The program executes the divide() function.
  • Traceback (most recent call last): ... ZeroDivisionError: division by zero: An error occurs while executing the divide() function. This is because you are dividing x by y, and y has a value of 0, resulting in a ZeroDivisionError.

Overall, the output is expected. The pdb debugger allows you to inspect the values of variables (x and y) before the error occurs and helps you identify issues in your code. In this case, it shows that the ZeroDivisionError is caused by dividing by zero, which you can address to fix the problem in your code.



Follow us on Facebook and Twitter for latest update.