pdb (python debugger) is a command-line debugging package for python that provides interactive source code debugging for python programs. The official documentation is linked at pdb - Python's Debugger。
The main functions of pdb include setting breakpoints, single-step debugging, entering function debugging, viewing the current code, viewing the values of variables and stack fragments, etc. This reduces or avoids the need to use print and log, which are cumbersome and tedious ways of debugging python code.
Installation and Usage
The pdb package is easy to install by piping it directly into your environment:
pip install pdb
The pdb package is also very simple to use, directly in the code where you want to enter the debugger insert the following two lines of command, and then save and re-execute the current python file can automatically jump to the location of the breakpoints, and then you can print out the relevant content for debugging.
import pdb pdb.set_trace() # Alternatively, the above two lines could be written on one line, separated by a ; sign. import pdb; pdb.set_trace()
pdb Common Commands
After adding the above two lines of commands to the code and running the program, the program will automatically jump to the location of the breakpoint where the code is located, and then you can check the corresponding variables in the terminal for debugging. After entering the pdb debug breakpoint mode, you can use commands to control the debugging and check the status of the program, some common commands in pdb are as follows:
command | account for |
---|---|
next or n | Keep running until you get to the next line of the current function |
step or s | Run the current line, stopping at the first position where you can stop (inside the called function or on the next line of the current function) |
continue or c | Continue executing the program until the next breakpoint is encountered |
list or l | List the source code of the current file |
p XXX | Print the value of a variable or expression (XXX is the name of the variable or expression) |
print(XXX) can also be used, but it is not a debugger command | It executes the print() function in Python to print the value of variable XXX. |
return or r | Continue executing the code until the current function returns |
quit or q | Exit the debugger and the executed program will be aborted. |
break or b | Setting breakpoints |
help | hand |
Debugging Code Example
The following is a simple list of lists items_list of all the elements in the sum and return code example, in order to demonstrate through the example, the blogger will list the elements of the sum of the sum written in the sum_list(items) function, and will be two elements of the sum (+) is written as a function sum_ab(a, b), the following code is the original code:
# Sum all elements of items and return def sum_list(items): items_sum = 0 for i in range(len(items_list)): item = items_list[i] items_sum = sum_ab(items_sum, item) return items_sum # Summing the two elements a and b def sum_ab(a, b): sum = a + b return sum if __name__ == "__main__": items_list = [1, 2, 3, 4, 5] items_sum = sum_list(items_list) print("The result of summing items_sum is:", items_sum)
In the above codesum_list(items)
Add debugging to the functionimport pdb; pdb.set_trace()
command to set a breakpoint.
# Sum all elements of items and return def sum_list(items): items_sum = 0 for i in range(len(items_list)): import pdb; pdb.set_trace() item = items_list[i] items_sum = sum_ab(items_sum, item) return items_sum # Summing the two elements a and b def sum_ab(a, b): sum = a + b return sum if __name__ == "__main__": items_list = [1, 2, 3, 4, 5] items_sum = sum_list(items_list) print("The result of summing items_sum is:", items_sum)
Starting Debugging: After running the program above that adds the pdb command, the program will stop at pdb.set_trace() and the left end of the command line will change from (base) to (pdb) indicating that you are currently in pdb debug mode.
Since items_list contains 5 elements, the for loop in the sum_list(items) function is executed 5 times and the result is returned to the main function.
The following process is the result of the execution of some common pdb commands, the blogger will be the role of each line of code through the # function of the form written on the right side of each line of code, to facilitate your understanding.
(base) PS F:\Code\ceshi> python ./ # Run the program that currently adds import pdb; pdb.set_trace() > f:\code\ceshi\(6)sum_list() -> item = items_list[i] # Execute the program until it stops at a breakpoint (this is in the first for loop). (Pdb) l The # l command lists the source code of the current file 1 # Sum all elements of items and return # -> indicates that the next line will be executed i.e. item=items_list[i], the previous line has been executed. 2 def sum_list(items): 3 items_sum = 0 4 for i in range(len(items_list)): 5 import pdb; pdb.set_trace() 6 -> item = items_list[i] 7 items_sum = sum_ab(items_sum, item) 8 return items_sum 9 10 # Summing the two elements a and b 11 def sum_ab(a, b): (Pdb) n # n command Continue to run until it reaches the next line of the current function (the current line item=items_list[i] has been executed and -> moved to the next line) > f:\code\ceshi\(7)sum_list() -> items_sum = sum_ab(items_sum, item) (Pdb) p item # p command prints the value of variable item (first for loop, item is = items_list[0]=1) 1 (Pdb) s # s command Runs the current line, stopping inside the called sum_ab(a,b) function. --Call-- > f:\code\ceshi\(11)sum_ab() -> def sum_ab(a, b): (Pdb) r # r command Continue executing code until current function sum_ab(a,b) returns --Return-- > f:\code\ceshi\(13)sum_ab()->1 -> return sum (Pdb) p sum # p command prints the value of the variable sum (sum is the value returned by sum_ab(a,b), the result of summing the first element of items_list[0]) 1 (Pdb) c # c command Continues to run, stopping only when it hits a breakpoint (jumps to 2nd for loop) > f:\code\ceshi\(5)sum_list() -> import pdb; pdb.set_trace() (Pdb) c # c command Continues to run, stopping only when it hits a breakpoint (jumps to 3rd for loop) > f:\code\ceshi\(6)sum_list() -> item = items_list[i] (Pdb) c # c command Continues to run, stopping only when it hits a breakpoint (jumps to the 4th for loop) > f:\code\ceshi\(5)sum_list() -> import pdb; pdb.set_trace() (Pdb) c # c command Continues to run, stopping only when it hits a breakpoint (jumps to the 5th for loop) > f:\code\ceshi\(6)sum_list() -> item = items_list[i] (Pdb) l The # l command lists the source code of the current file 1 # Sum all elements of items and return 2 def sum_list(items): 3 items_sum = 0 4 for i in range(len(items_list)): 5 import pdb; pdb.set_trace() 6 -> item = items_list[i] 7 items_sum = sum_ab(items_sum, item) 8 return items_sum 9 10 # Summing the two elements a and b 11 def sum_ab(a, b): (Pdb) n # n command Continue until you reach the next line of the current function (-> move to the next line) > f:\code\ceshi\(7)sum_list() -> items_sum = sum_ab(items_sum, item) (Pdb) p item # p command prints the value of variable item (5th for loop) 5 (Pdb) n # n command Continue until you reach the next line of the current function (-> move to the next line) > f:\code\ceshi\(4)sum_list() -> for i in range(len(items_list)): (Pdb) p items_sum # p command prints the value of the variable items_sum (after the 5th for loop: the total value obtained by adding all the elements) 15 (Pdb) q # q Traceback (most recent call last): File "F:\Code\ceshi\", line 17, in <module> items_sum = sum_list(items_list) File "F:\Code\ceshi\", line 4, in sum_list for i in range(len(items_list)): File "F:\Code\ceshi\", line 4, in sum_list for i in range(len(items_list)): File "D:\Tools\Anaconda3\SetUp\lib\", line 88, in trace_dispatch return self.dispatch_line(frame) File "D:\Tools\Anaconda3\SetUp\lib\", line 113, in dispatch_line if : raise BdbQuit (base) PS F:\Code\ceshi>
The above is Python breakpoint debugging pdb package usage details, more information about Python breakpoint debugging pdb please pay attention to my other related articles!