7 Need to Know Python pdb Debugger Commands

Here are seven helpful python pdb commands. Those are next, quit, print line numbers, display variable, set break points and clear break points.

Advertisements

Here’s Python source code to use for debugging

#Python source code is present # in main.py
import pdb;
pdb.set_trace()

#try finally
try:
print("Hello World")
# No luck, an exception
number = int("abc")
finally:
print("==> Let's start our journey to the moon!")

print("TO THE MOON…")

Before you start debugging, firstly import pdb. Then set pdb to set_trace(). The set_trace() allows you to debug your code interactively.

List of python pdb commands

n –> to go to next stmt

q —> to quit from debug mode

l —> to print line numbers

p –> display variable

b—> b x set b point

cl –> clear

c –> continue

Result of each command of pdb debugger

Python pdb Command n

Here, you will see the result of ‘n’ command. It goes to next statement and execute it.

> /home/repl/b7e783ae-efe2-4425-ac87-ed04cfce52d6/main.py(5)<module>() 
-> try:
(Pdb)
n
> /home/repl/b7e783ae-efe2-4425-ac87-ed04cfce52d6/main.py(6)<module>()
-> print("Hello World")
(Pdb)

Python pdb Command q

You will see here the result of ‘q’ command. The control comes out of the debugger.

> /home/repl/ef97b988-d2f3-44b5-97f5-c83110b99b76/main.py(5)<module>() 
-> try:
(Pdb)
q
Traceback (most recent call last):
File "main.py", line 5, in <module>
try:
File "main.py", line 5, in <module>
try:
File "/usr/lib/python3.8/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib/python3.8/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit


** Process exited - Return Code: 1 **

Python pdb Commands l (print line numbers) and c (continue)

The command l displays line numbers for source code.

> /home/repl/6b3c467d-30ac-477f-ab1e-fe07b185a710/main.py(5)<module>() 
-> try:
(Pdb)
l
1 import pdb;
2 pdb.set_trace()
3
4 #try finally
5 -> try:
6 print("Hello World")
7 # No luck, an exception
8 number = int(123)
9 finally:
10 print("==> Let's start our journey to the moon!")
11
(Pdb)
c
Hello World
==> Let's start our journey to the moon!
TO THE MOON…


** Process exited - Return Code: 0 **

Python Command p

The command p and variable name if you give, it displays the value present in the variable.

> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(5)<module>() 
-> try:
(Pdb)
n
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(6)<module>()
-> print("Hello World")
(Pdb)
n
Hello World
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(8)<module>()
-> number = int(123)
(Pdb)
n
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(10)<module>()
-> print("==> Let's start our journey to the moon!")
(Pdb)
p number
123
(Pdb)
c
==> Let's start our journey to the moon!
TO THE MOON…


** Process exited - Return Code: 0 **

Command b (break point) and Command cl (clears all break points)

Here, when you set b and line number, it sets break point at that line. The cl command clears all break points. I set break point at line 8. When I give c (continue), it will then stops at line 8.


> /home/repl/cb23d974-ca23-4158-bef1-c16f7f9b0183/main.py(5)<module>()
-> try:
(Pdb)
l
1 import pdb;
2 pdb.set_trace()
3
4 #try finally
5 -> try:
6 print("Hello World")
7 # No luck, an exception
8 number = int(123)
9 finally:
10 print("==> Let's start our journey to the moon!")
11
(Pdb)
b 8
Breakpoint 1 at /home/repl/cb23d974-ca23-4158-bef1-c16f7f9b0183/main.py:8
(Pdb)
c
Hello World
> /home/repl/cb23d974-ca23-4158-bef1-c16f7f9b0183/main.py(8)<module>()
-> number = int(123)
(Pdb)

Python Clear cl command

> /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py(5)<module>() 
-> try:
(Pdb)
b 8
Breakpoint 1 at /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py:8
(Pdb)
c
Hello World
> /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py(8)<module>()
-> number = int(123)
(Pdb)
cl
Clear all breaks?
y
Deleted breakpoint 1 at /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py:8
(Pdb)
c
==> Let's start our journey to the moon!
TO THE MOON…


** Process exited - Return Code: 0 **
Press Enter to exit terminal

To sum up, these seven commands are useful to work with python pdb debugger.

References

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.