05: Python debugging tutorial

Q. How would you go about debugging Python code?
A. Firstly, import pdb

Secondly, use “pdb.set_trace()” where ever you want the execution to pause as shown below with “addition.py” file.

Thirdly, run your code as

When your program encounters the line with pdb.set_trace() it will start tracing. Execute the next statement… with “n” (next). Repeating the last debugging command… with ENTER. Printing the value of variables… with “p” (print)

c continue execution
w shows the context of the current line it is executing.
a print the argument list of the current function
s Step into subroutines.
r Continuing… but just to the end of the current subroutine.
n Continue execution until the next line in the current function is reached or it returns.
l Show where you are on the code
ENTER Repeat the last command.

Python 3.7 implements PEP553

A new way of inserting breakpoints in your code with breakpoint() method call. The default implementation of breakpoint will import pdb and call pdb.set_trace() .

The new breakpoint method can be disabled using the PYTHONBREAKPOINT environment variable.

PuDB – Python Debugging Tool

If you don’t have easy_install, download the package from the PuDB home page, unpack it, go to the resulting directory and type

web-pdb – remote debugger

If you’re developing an application for distribution across multiple servers or threads, you can use a local debugger like pdb or pudb. Web-pdb is a remote debugger that starts a web server when the breakpoint hits and you can remotely connect via a browser and interactively debug the session.

800+ Java & Big Data Interview Q&As