In Python, syntax errors can be found by the Python interpreter, but logical errors or errors in the use of variables are not easy to find, if the results do not meet expectations, you need to debug, a good debugging tool:Python comes with the pdb module. pdb is a debugging module that comes with Python. Using the pdb module, you can set breakpoints for scripts, single-step execution, view variable values, and so on.
pdb can be started with a command line argument, or imported and then used.
>>> dir(pdb)
['Pdb', 'Repr', 'Restart', 'TESTCMD',.....,'re', 'run', 'runcall', 'runctx', 'runeval', 'set_trace', 'sys', 'test', 'traceback']
Common pdb functions are the following.
[() function]
>>> This function is mainly used for debugging statement blocks.
>>> The basic usage is as follows
>>> help()
Help on function run in module pdb:
run(statement, globals=None, locals=None)
>>>Parameter Meaning
statement: The statement block to be debugged, as a string.
globals: optional parameter to set the global environment variables for the statement to run.
locals: optional parameter to set the local environment variables for the statement to run.
>>>Simple Example
>>> import pdb # import debug module
>>> (''''' # call the run() function to execute a for loop
for i in range(3):
i *= 3
print(i)
''')
> <string>(2)<module>()
(Pdb) n # (Pdb) is a debugging command prompt, indicating that debugging commands can be entered
> <string>(3)<module>()
(Pdb) n # n(next) means execute next line
> <string>(4)<module>()
(Pdb) print(i) # Print the value of variable i
0
(Pdb) continue # Continue to run the program
0
3
6
[() function]
>>> This function is mainly used for debugging the expression
>>> The basic usage is as follows
>>> help()
Help on function runeval in module pdb:
runeval(expression, globals=None, locals=None)
>>> Parameter Meaning
expression: the one to be debugged.
globals: optional parameter to set the global environment variables for the statement to run.
locals: optional parameter to set the local environment variables for the statement to run.
>>> Simple Example
>>> import pdb # import pdb module
>>> lst = [1, 2, 3] # define a list
>>> ('lst[1]') # Call runaval() function to debug expression lst[1]
> <string>(1)<module>()
(Pdb) n # Enter debug state, use n command, single-step execution
--Return--
> <string>(1)<module>()->2
(Pdb) n # Single-step execution
2 # Returns the value of the expression
>>> ('3 + 5*6/2') # Use the runaval() function to debug the expression 3 + 5*6/2
> <string>(1)<module>()->2
(Pdb) n
--Return--
> <string>(1)<module>()->18
(Pdb) n # Single-step execution using the n command
18 # Finalize the value of the expression
[() function]
>>> This function is mainly used to debug the function
>>> The basic usage is as follows
>>> help()
Help on function runcall in module pdb:
runcall(*args, **kwds)
>>> Parameter Meaning
function: function name
args(kwds): arguments to the function
>>> Simple Example
>>> import pdb # Import modules
>>> def sum(*args): # Define the sum function to sum all arguments.
res = 0
for arg in args:
res += arg
return res
>>> (sum, 1, 2, 3, 4) # Use runcall to debug the function sum
> <pyshell#53>(2)sum()
(Pdb) n # Enter debug state, single-step execution
> <pyshell#53>(3)sum()
(Pdb) n # Single-step execution
> <pyshell#53>(4)sum()
(Pdb) print(res) # print the value of res using print
0
(Pdb) continue # Continue
10
>>> (sum, 1, 2, 3, 4, 5, 6) # call runcall debug function sum with different arguments
> <pyshell#53>(2)sum()
(Pdb) continue # Continue
21 # Function final return result
[pdb.set_trace() function]
>>> This function is mainly used to set hard breakpoints in scripts
>>> The basic usage is as follows
>>> help(pdb.set_trace)
Help on function set_trace in module pdb:
set_trace()
>>>Simple Example
# file:
import pdb
pdb.set_trace()
for i in range(5):
i *= 5
print(i)
Running the script displays.
> d:\learn\python\(6)<module>()
-> for i in range(5):
(Pdb) list # Use list to list the contents of a script.
1 # file:
2
3 import pdb
4
5 pdb.set_trace() # Use set_trace() to set hard breakpoints
6 -> for i in range(5):
7 i *= 5
8 print(i)
[EOF] # End of listing script contents
(Pdb) continue # Continue execution using continue
0
5
10
15
20
[pdb debugging commands]
The debugging commands in pdb can accomplish single-step execution, print variable values, set breakpoints, and more. pdb's main commands are as follows
------------------------------------------------------------------------------
# Full Command Shortened Command Description
------------------------------------------------------------------------------
# args a prints the arguments of the current function
# break b Set breakpoints
# clear cl Clear breakpoints
# condition None Set condition breakpoint
# continue c Continue to run until a breakpoint is encountered or the script ends
# disable None Disable breakpoints
# enable None Enable breakpoints
# help h View pdb help
# ignore None Ignore breakpoints
# jump j Jump to the specified line number to run
# list l List scripts
# next n Execute the next statement, encountering the function without going inside it
# print p Prints the value of the variable
# quit q quit pdb
# return r Runs consistently until the function returns
# tbreak None Set temporary breakpoints, breakpoints only break once
# step s Execute the next statement that encounters the function into its interior
# where w View location
# ! None Execute the statement in pdb
>>>Simple Example
# -*- coding:gbk -*-
# file:
#
import math
# isprime function determines whether an integer is prime or not
# If i is divisible by any number from 2 to the square root of i, then
# Returns 0 if i is not a prime, otherwise returns 1 if i is a prime.
def isprime(i):
for t in range(2, int((i)) + 1):
if i % t == 0:
return 0
print('The prime numbers between 100 and 110 are: ')
for i in range(100, 110):
if isprime(i):
print(i)
Run the following command first:
d:\Learn\Python>python -m pdb
After that, enter the following command:
d:\Learn\Python>python -m pdb
> d:\learn\python\(4)<module>()
-> import math
(Pdb) list # Stop here after running the previous command, list only lists 11 lines by default
1 # -*- coding:gbk -*-
2 # file:
3 #
4 -> import math
5 # isprime function determines whether an integer is prime or not
6 # If i is divisible by any number from 2 to the square root of i.
7 # Returns 0 if i is not a prime, otherwise returns 1 if i is a prime.
8 def isprime(i):
9 for t in range(2, int((i)) + 1):
10 if i % t == 0:
11 return 0
(Pdb) l 14,17 # Use the list command to list lines 14 through 17
14 print('The prime numbers between 100 and 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 a breakpoint
Breakpoint 1 at d:\learn\python\:14 # Return breakpoint number: 1
(Pdb) b isprime # Set a breakpoint in the function isprime
Breakpoint 2 at d:\learn\python\:8 # Return breakpoint number: 2
(Pdb) c # Run the run script with the c command
> d:\learn\python\(14)<module>() # Stop at breakpoint 1, line 14.
-> print('The prime numbers between 100 and 110 are: ')
(Pdb) c # Continue to run the script using the c command
The prime numbers between 100 and 110 are: # Script output on line 14
> d:\learn\python\(9)isprime() # Stop at breakpoint 2, which is the isprime function
-> for t in range(2, int((i)) + 1):
(Pdb) b 15 # Set breakpoint at line 15
Breakpoint 3 at d:\learn\python\:15
(Pdb) disable 2 # Disable breakpoint 2, the breakpoint at the isprime function.
(Pdb) c # Continue to run the script using the c command
> d:\learn\python\(15)<module>() # Stop at breakpoint 3, line 15.
-> for i in range(100, 110):
(Pdb) print(i) # Use print to print the value of variable i
100
(Pdb) c # Continue to run scripts
> d:\learn\python\(15)<module>()
-> for i in range(100, 110):
(Pdb) p i # print the value of i
101
(Pdb) enable 2 # Restore breakpoint 2, the breakpoint at the isprime function.
(Pdb) c # Continue to run scripts
> d:\learn\python\(9)isprime()
-> for t in range(2, int((i)) + 1):
(Pdb) n # Single-step execution of the next statement
> d:\learn\python\(10)isprime()
-> if i % t == 0:
(Pdb) print(t) # Use print to print the value of the variable t
2
(Pdb) cl # Clear all breakpoints, enter y to confirm.
Clear all breaks? y
(Pdb) c # Continue to run scripts
103
105
107
109
(Pdb) q # Exit pdb debugging with quit(q)