This article discusses how to use PDB to debug Python without convenient ide tools.Program
Source code example
For example, there is a program to simulate tax calculation:
#! /Usr/bin/Python
Def debug_demo (VAL ):
If Val <= 1600:
Print "Level 1"
Print 0
Elif Val <= 3500:
Print "Level 2"
Print (Val-1600) * 0.05
Elif Val <= 6500:
Print "Level 3"
Print (Val-3500) * 0.10 + (3500-1600) * 0.05
Else:
Print "Level 4"
Print (Val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
#~ Def debug_demo
If _ name _ = "_ main __":
Debug_demo (4500)
The debug_demo function calculates the tax required for 4500 of the account.
How to debug?
1. Add a breakpoint
Add the red part to the place where the breakpoint needs to be inserted.Code: If the value of _ debug is true, debug is started here (the reason for adding _ debug is to enable/disable debugging conveniently ).
#! /Usr/bin/Python
_ DEBUG = true
Def debug_demo (VAL ):
If _ DEBUG = true:
Import PDB
PDB. set_trace ()
If Val <= 1600:
Print "Level 1"
Print 0
Elif Val <= 3500:
Print "Level 2"
Print (Val-1600) * 0.05
Elif Val <= 6500:
Print "Level 3"
Print (Val-3500) * 0.10 + (3500-1600) * 0.05
Else:
Print "Level 4"
Print (Val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
#~ Def debug_demo
If _ name _ = "_ main __":
Debug_demo (4500)
2. Start running debugging
Run the program./debug_demo.py.
>/Usr/local/qspace/user_network/debug_demo.py (7) debug_demo ()
-> If Val <= 1600:
(PDB)
-> Val <= 1600: indicates the statement currently executed. (PDB) the number of commands waiting for your debugging command. PDB is very rich. You can enter the H command to view how to use the command. The following describes common commands:
View the code context, L (lower case l)
(PDB) L
2 _ DEBUG = true
3 def debug_demo (VAL ):
4 if _ DEBUG = true:
5 import PDB
6 PDB. set_trace ()
7-> If Val <= 1600:
8 print "Level 1"
9 print 0
10 Elif Val <= 3500:
11 print "Level 2"
12 print (Val-1600) * 0.05
(PDB)
The row number on the left and the code body on the right.
Monitoring variable: P variable name
(PDB) P Val
4500
(PDB)
One-step execution: N
-> Elif Val <= 3500:
(PDB) L
5 import PDB
6 PDB. set_trace ()
7 if Val <= 1600:
8 print "Level 1"
9 print 0
10-> Elif Val <= 3500:
11 print "Level 2"
12 print (Val-1600) * 0.05
13 Elif Val <= 6500:
14 print "Level 3"
15 print (Val-3500) * 0.10 + (3500-1600) * 0.05
Add breakpoint: B row number
(PDB) B 14
Run to breakpoint: c
(PDB) c
> /*****
-> Print "Level 3"
(PDB) L
9 print 0
10 Elif Val <= 3500:
11 print "Level 2"
12 print (Val-1600) * 0.05
13 Elif Val <= 6500:
14 B-> Print "Level 3"
15 print (Val-3500) * 0.10 + (3500-1600) * 0.05
16 else:
17 print "Level 4"
18 print (Val-6500) * 0.20 + (6500-3500) * 0.10 + (3500-1600) * 0.05
19
Before the function is returned: R
(PDB) r
Level 3
195.0
-- Return --
> /****()
-> None
-> Print (Val-3500) * 0.10 + (3500-1600) * 0.05
(PDB)
Note:
PDB also has many other useful commands that readers can explore on their own. Enter the H and H commands. You can get the Detailed Help of the command.
However, I personally think that this debugging method does not need to be started. Generally, you can use log output for debugging unless you encounter a very subtle error. Then, the power of one-step debugging is displayed.