This article mainly introduces how to use the pdb module to debug Python program instances. This article focuses on pdb. run () function, pdb. runeval () function, pdb. runcall () function, pdb. the use of set_trace () functions and pdb debugging commands can be found by the Python interpreter, however, logic errors or variable usage errors are not easy to find. if the results do not conform to expectations, debugging is required. a good debugging tool: the pdb module of Python. Pdb is a Python debugging module. The pdb module allows you to set breakpoints, perform one-step operations, and view variable values for scripts.
Pdb can be started using command line parameters, or imported before use.
The code is as follows:
>>> Dir (pdb)
['Pdb', 'repr', 'restart', 'testcmd ',....., 're', 'run', 'runcall', 'runctx ', 'runneval', 'set _ track', 'sys ', 'test', 'traceback']
Common pdb functions include the following:
[Pdb. run () function]
>>> This function is mainly used to debug statement blocks.
>>> Basic usage:
The code is as follows:
>>> Help (pdb. run)
Help on function run in module pdb:
Run (statement, globals = None, locals = None)
>>> Parameter description
Statement: statement block to be debugged, expressed as a string
Globals: an optional parameter that sets the global environment variable for statement running.
Locals: an optional parameter that sets the local environment variable for statement running.
>>> Simple example
The code is as follows:
>>> Import pdb # import and debug module
>>> Pdb. run (''' # Call the run () function to execute a for loop
For I in range (3 ):
I * = 3
Print (I)
''')
> (2) ()
(Pdb) n # (Pdb) is the debugging command prompt, indicating that you can enter the debugging command
> (3) ()
(Pdb) n # n (next) indicates executing the next row
> (4) ()
(Pdb) print (I) # print the value of variable I
0
(Pdb) continue # continue to run the program
0
3
6
[Pdb. runeval () function]
>>> This function is mainly used for debugging expressions.
>>> Basic usage:
The code is as follows:
>>> Help (pdb. runeval)
Help on function runeval in module pdb:
Runeval (expression, globals = None, locals = None)
>>> Parameter description
Expression: The object to be debugged,
Globals: an optional parameter that sets the global environment variable for statement running.
Locals: an optional parameter that sets the local environment variable for statement running.
>>> Simple example
The code is as follows:
>>> Import pdb # import the pdb module
>>> Lst = [1, 2, 3] # define a list
>>> Pdb. runeval ('lst [1] ') # Call the runaval () function to debug the expression lst [1]
> (1) ()
(Pdb) n # enter the debugging status. run the n command in one step.
-- Return --
> (1) ()-> 2
(Pdb) n # One-Step execution
2 # return the expression value
>>> Pdb. runeval ('3 + 5*6/2 ') # Use the runaval () function to debug expressions 3 + 5*6/2
> (1) ()-> 2
(Pdb) n
-- Return --
> (1) ()-> 18
(Pdb) n # run the n command in one step
18 # obtain the final expression value
[Pdb. runcall () function]
>>> This function is mainly used to debug functions.
>>> Basic usage:
The code is as follows:
>>> Help (pdb. runcall)
Help on function runcall in module pdb:
Runcall (* args, ** kwds)
>>> Parameter description
Function: function name
Args (kwds): function parameter
>>> Simple example
The code is as follows:
>>> Import pdb # import Module
>>> Def sum (* args): # defines the sum function and calculates the sum of all parameters.
Res = 0
For arg in args:
Res + = arg
Return res
>>> Pdb. runcall (sum, 1, 2, 3, 4) # use runcall to debug the sum function
> (2) sum ()
(Pdb) n # enter the debugging status, one-step execution
> (3) sum ()
(Pdb) n # One-Step execution
> (4) sum ()
(Pdb) print (res) # print the res value using print
0
(Pdb) continue # continue execution
10
>>> Pdb. runcall (sum, 1, 2, 3, 4, 5, 6) # Call the runcall debug function sum. the parameters are different.
> (2) sum ()
(Pdb) continue # continue execution
21 # Final result returned by the function
[Pdb. set_trace () function]
>>> This function is mainly used to set hard breakpoints in scripts.
>>> Basic usage:
The code is as follows:
>>> Help (pdb. set_trace)
Help on function set_trace in module pdb:
Set_trace ()
>>> Simple example
The code is as follows:
# File: test. py
Import pdb
Pdb. set_trace ()
For I in range (5 ):
I * = 5
Print (I)
After running the script, it is displayed as follows:
The code is as follows:
> D: \ learn \ python \ test. py (6) ()
-> For I in range (5 ):
(Pdb) list # use list to list script content
1 # file: test. py
2
3 import pdb
4
5 pdb. set_trace () # use set_trace () to set a hard breakpoint
6-> for I in range (5 ):
7 I * = 5
8 print (I)
[EOF] # End of listing script content
(Pdb) continue # use continue to continue execution
0
5
10
15
20
[Pdb debugging command]
The debugging commands in pdb can be used to perform single-step execution, print variable values, and set breakpoints. The main commands for pdb are as follows:
The code is as follows:
------------------------------------------------------------------------------
# Complete command shorthand command description
------------------------------------------------------------------------------
# Args a prints the parameters of the current function
# Break B sets breakpoints
# Clear cl clear breakpoints
# Condition no set condition breakpoint
# Continue c continues running until a breakpoint or script ends
# Disable
# Enable
# Help h view pdb help
# Ignore does not ignore breakpoints
# Jump to the specified number of lines to run jump j
# List l list scripts
# Next n: execute the next statement. if a function is not entered
# Print p print the variable value
# Quit q quit pdb
# Return r consistently run to function return
# Tbreak: no temporary breakpoint is set, and the breakpoint is interrupted only once.
# Step s: execute the next statement and enter the function
# Where w: view the location
#! No statement executed in pdb
>>> Simple example
The code is as follows:
#-*-Coding: gbk -*-
# File: prime. py
#
Import math
# The isprime function checks whether an integer is a prime number.
# If I can be divisible by any number in the square root of 2 to I,
# If I is not a prime number, 0 is returned. if I is a prime number, 1 is returned.
Def isprime (I ):
For t in range (2, int (math. sqrt (I) + 1 ):
If I % t = 0:
Return 0
Print ('2017 ~ The prime numbers between 110 are :')
For I in range (100,110 ):
If isprime (I ):
Print (I)
Run the following command first:
The code is as follows:
D: \ Learn \ Python> python-m pdb prime. py
Then enter the following command:
The code is as follows:
D: \ Learn \ Python> python-m pdb prime. py
> D: \ learn \ python \ prime. py (4) ()
-> Import math
(Pdb) list # Stop here after running the preceding command. by default, list only lists 11 rows.
1 #-*-coding: gbk -*-
2 # file: prime. py
3 #
4-> import math
5 # isprime function to determine whether an integer is a prime number
6 # if I can be divisible by any number in the square root of 2 to I,
7 # if I is not a prime number, 0 is returned. Otherwise, I is a prime number and 1 is returned.
8 def isprime (I ):
9 for t in range (2, int (math. sqrt (I) + 1 ):
10 if I % t = 0:
11 return 0
(Pdb) l 14,17 # Use the list command to list 14 rows to 17 rows
14 print ('2017 ~ The prime numbers between 110 are :')
15 for I in range (100,110 ):
16 if isprime (I ):
17 print (I)
(Pdb) B 14 # use the break command to set breakpoints
Breakpoint 1 at d: \ learn \ python \ prime. py: 14 # return Breakpoint No.: 1
(Pdb) B isprime # set a breakpoint in the isprime function
Breakpoint 2 at d: \ learn \ python \ prime. py: 8 # return Breakpoint No.: 2
(Pdb) c # run the script using the c command
> D: \ learn \ python \ prime. py (14) () # Stop at the breakpoint 1, that is, 14th rows
-> Print ('2017 ~ The prime numbers between 110 are :')
(Pdb) c # use the c command to continue running the script
100 ~ The prime numbers between 110 are: # 14th line script output
> D: \ learn \ python \ prime. py (9) isprime () # stop at breakpoint 2, that is, at the isprime function
-> For t in range (2, int (math. sqrt (I) + 1 ):
(Pdb) B 15 # set a breakpoint on the 15th line
Breakpoint 3 at d: \ learn \ python \ prime. py: 15
(Pdb) disable 2 # disable breakpoint 2, that is, the breakpoint at the isprime function
(Pdb) c # use the c command to continue running the script
> D: \ learn \ python \ prime. py (15) () # Stop at 3 of the breakpoint, that is, 15th rows.
-> For I in range (100,110 ):
(Pdb) print (I) # print the value of variable I with print
100
(Pdb) c # continue to run the script
> D: \ learn \ python \ prime. py (15) ()
-> For I in range (100,110 ):
(Pdb) p I # print the I value
101
(Pdb) enable 2 # restore breakpoint 2, that is, the breakpoint at the isprime function
(Pdb) c # continue to run the script
> D: \ learn \ python \ prime. py (9) isprime ()
-> For t in range (2, int (math. sqrt (I) + 1 ):
(Pdb) n # execute the next statement in one step
> D: \ learn \ python \ prime. py (10) isprime ()
-> If I % t = 0:
(Pdb) print (t) # print the value of variable t with print
2
(Pdb) cl # clear all breakpoints, input y to confirm
Clear all breaks? Y
(Pdb) c # continue to run the script
103
105
107
109
(Pdb) q # Use quit (q) to exit pdb debugging