Discussion RoomCategory: Interview QuestionHow to use pdb in python?
Ashly asked 5 years ago

Step 1: Load pdb module and insert set_trace() where you want to start watching the execution.
Step 2: Run the code.
You can run the code anywhere, be it in command line using python sample.py, a python console like IDLE or pretty much anywhere you can run python.
Let’s add the pdb.set_trace() in the first line of samplefunc().

# sample.py
import pdb

def add(a, b):
    return a + b

def samplefunc():
    pdb.set_trace()  # -- added breakpoint
    var = 10
    print("One. Line 1 ..")
    print("Two. Line 2 ..!")
    out = add("summy", var)
    print('Three. Line 3 .. Done!')
    return out

$ python3 sample.py
-> var = 10

Upon running the code, pdb’s debugger console starts where set_trace() is placed, waiting for your instructions.
The arrow mark points to the line that will be run next by pdb.