Python pdb module and breakpoint() for debugging

Ckmobile
3 min readDec 7, 2022
Photo by Sigmund on Unsplash

In the old days, we used import pdb and pdb.set_trace() to debug

For example, we create a file called ‘main.py’ and enter the following.

import pdb

def hello():
print('hello1')
print('hello2')

y = 2
z = 3
x=True
pdb.set_trace()
print(y+z)
print(x+x)
hello()
pdb.set_trace()
print(x+y)

But now, we just need to use breakpoint() replace pdb.set_trace().

def hello():
print('hello1')
print('hello2')

y = 2
z = 3
x=True
breakpoint()
print(y+z)
print(x+x)
hello()
breakpoint()
print(x+y)

The followings are the command for debugger

h: help
w: where
n: next
s: step (steps into function)
c: continue
p: print
l: list
q: quit
b: show all break points
b 8: set break point at line 8
b hello: set break point at function 'hello'
cl: clear all break points
cl 8: clear break point at line 8

Go to terminal and type ‘python main.py’

For example, we can type h to help and w to see where this file is.

--

--