Header Ads

What is pdb Python debugger?

 Module pdb defines an interactive source code debugger for Python programs. Supports setting (conditional) break intervals and single step-level at source code, stack independent testing, source code listing, and random Python code testing in the context of any stack. It also supports post-mortem debugging and can be called under program control.

Read More

How to use pdb Python Debugger 

Sample code I am using
bike1="yamaha"
bike1_price=250000
bike2="honda"
bike2_price=500000

name=input("enter your name ")
choose=int(input("press 1 for yahama or 2 for honda: "))
print("hello",(name))
if choose==1:
    print((bike1),"will cost you",(bike1_price))
elif choose==2:
    print((bike2),"will cost you",(bike2_price+2000))
else:
    print("you enter wrong input")

Output of the code :

1. If i press 1
enter your name : Abhi
press 1 for yahama or 2 for honda : 1
hello Abhi
yamaha will cost you 250000

2. If i press 2
enter your name : Abhi
press 1 for yahama or 2 for honda : 2
hello Abhi
honda will cost you 502000

Now, I will intentionally make a error in this code and check how debugging works 
bike1="yamaha"
bike1_price=25000
bike2="honda"
bike2_price=50000

name=input("enter your name : ")
choose=int(input("press 1 for yahama or 2 for honda : "))
print("hello",(name))
if choose==1:
    print((bike1),"will cost you",(bike1_price))
elif choose==2:
    print((bike2+2000),"will cost you",(bike2_price))
else:
    print("you enter wrong input")

Method 1 : By using terminal 

1. Open terminal in the source code folder
2. Enter the commands as follow

python -m pdb filename.py

"Enter the above command and press Enter"

PS E:\Step Towards Coding\Python> python -m pdb p_d_b.py    
> e:\step towards coding\python\p_d_b.py(1)<module>()
-> bike1="yamaha"
(Pdb)

"If you will input 'l' it will point the cursor where the debugger is reading"

(Pdb) l
  1  -> bike1="yamaha"
  2     bike1_price=25000
  3     bike2="honda"
  4     bike2_price=50000
  5
  6     name=input("enter your name : ")
  7     choose=int(input("press 1 for yahama or 2 for honda : "))
  8     print("hello",(name))
  9     if choose==1:
 10         print((bike1),"will cost you",(bike1_price))
 11     elif choose==2:
(Pdb)

"If You will Input 'c' it will execute the next line"

(Pdb) c
enter your name :

"Keep Executing the lines until it reaches the error"

enter your name : Abhi
press 1 for yahama or 2 for honda :

"When you will input '2' here, Error is caught"

File "C:\Users\abhay\AppData\Local\Programs\Python\Python310\lib\bdb.py", line 597, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "e:\step towards coding\python\p_d_b.py", line 12, in <module>
    print((bike2+2000),"will cost you",(bike2_price))
TypeError: can only concatenate str (not "int") to str
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> e:\step towards coding\python\p_d_b.py(12)<module>()
-> print((bike2+2000),"will cost you",(bike2_price))
(Pdb)

"Now you can try correcting error here, i have corrected the error and it has given the desired output"

(Pdb) print((bike2),"will cost you",(bike2_price))      
honda will cost you 50000
(Pdb) 

"Error is corrected, you can do the correction in source code"
"If you Input 'c' again it will restart the program, you can also use 'cont' or 'step' to restart the program"
"Use 'q' to exit the debugger"

(Pdb) q
PS E:\Step Towards Coding\Python> 

Method 2 : By importing pdb module in the source code

Import pdb module in the program and set the trace using
"pdb.set_trace()"
and run the program, it will run in debugging mode from where the trace is set and rest procedure is same as in method 1.
import pdb #importing pdb

bike1 = "yamaha"
bike1_price = 25000
bike2 = "honda"
bike2_price = 50000

pdb.set_trace() #setting trace

name = input("enter your name : ")
choose = int(input("press 1 for yahama or 2 for honda : "))
print("hello", (name))
if choose == 1:
    print((bike1), "will cost you", (bike1_price))
elif choose == 2:
    print((bike2+2000), "will cost you", (bike2_price))
else:
    print("you enter wrong input")


Debugging is useful to find errors in the long programs where it is difficult to find the errors manually.
If you want to know more about pdb module you can contact me or read the docs at python.org

Post a Comment

0 Comments