The debug feature is very important for developer, and Python provides the appropriate module PDB so that you can debug with a text editor to write scripts. The PDB is the abbreviation for Python debugger.
Some of the commonly used commands are:
Command |
Use |
Break or B |
Set breakpoints |
Continue or C |
Continue to execute the program |
List or L |
View the code snippet for the current row |
Step or S |
Enter function |
Return or R |
Executes the code until it returns from the current function |
Exit or Q |
Abort and exit |
Next or N |
Executes the next line |
Pp |
Print the value of a variable |
Help |
Help |
Start by describing how to use the PDB.
Test code used by 1:epdb1.py
Import PDB
A = "AAA"
Pdb.set_trace ()
b = "BBB"
c = "CCC"
Final = a + B + C
Print final
About Set_trace ()
PDB. Set_trace ()¶
-
Enter the debugger at the calling stack frame. This was useful to Hard-code abreakpoint at a given point in a program, even if the code was not otherwisebeing debugged (E. G. When an assertion fails).
1 Start Debugging:
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB)
>/root/epdb1.py (6)? ()
Final = a + B + C
(PDB) List
1 Import PDB
2 A = "AAA"
3 Pdb.set_trace ()
4 B = "BBB"
5 c = "CCC"
6-final = a + B + C
7 Print Final
[EOF]
(PDB)
[EOF]
(PDB) n
>/root/epdb1.py (7)? ()
Print final
(PDB)
- Using N+enter to perform the current statement, you can press ENTER to repeat the previous debug command after the first time you press the N+enter.
If you press ENTER without entering anything, the PDB would re-execute the last command that you gave it.
- Quit or Q can exit the current debug, but quit will exit the program in a very rude way, directly crash
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB) Q
Traceback (most recent):
File "epdb1.py", line 5, in?
c = "CCC"
File "epdb1.py", line 5, in?
c = "CCC"
File "/usr/lib64/python2.4/bdb.py", line +, in Trace_dispatch
Return Self.dispatch_line (frame)
File "/usr/lib64/python2.4/bdb.py", line-up, in Dispatch_line
If Self.quitting:raise bdbquit
BdB. Bdbquit
- In the process of printing the value of the variable, you can directly use p plus the variable name, but it should be noted that printing only after the current statement has been executed to see the specific value, otherwise it will be reported Nameerror: <exceptions. Nameerror. > Error.
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB) P b
' BBB '
(PDB)
' BBB '
(PDB) n
>/root/epdb1.py (6)? ()
Final = a + B + C
(PDB) P C
' CCC '
(PDB) P final
Nameerror: <exceptions. Nameerror instance at 0x1551b710>
(PDB) n
>/root/epdb1.py (7)? ()
Print final
(PDB) P final
' AAABBBCCC '
(PDB)
Use C to stop the current debug so that the program continues execution. If you continue to have set_statement () in the following program, you will re-enter the state of Debug.
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB) C
Aaabbbccc
You can add set_trace () validation before the code print final.
- If the code procedure does not necessarily remember that the current code is fast at debug time, it can be displayed by using the list or the L command. List will point to the current debug statement
with arrows
[[Email protected]c-pok-idg-2255 ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) List
1 Import PDB
2 A = "AAA"
3 Pdb.set_trace ()
4-B = "BBB"
5 c = "CCC"
6 final = a + B + C
7 Pdb.set_trace ()
8 Print Final
[EOF]
(PDB) C
>/root/epdb1.py (8)? ()
Print final
(PDB) List
3 Pdb.set_trace ()
4 B = "BBB"
5 c = "CCC"
6 final = a + B + C
7 Pdb.set_trace ()
Print Final, 8
[EOF]
(PDB)
For debug with a function:
epdb2.py--import pdbdef Combine (s1,s2): # define subroutine Combine, which ... S3 = S1 + s2 + S1 # sandwiches S2 between copies of S1, ... S3 = ' "' + s3 + '" ' # encloses it in double quotes,... Return S3 # and returns IT.A = "AAA" Pdb.set_trace () b = "BBB" c = "CCC" final = Combine (A, B) print final
If you use N for debug, it will be treated as a normal assignment statement when you go to Final=combine, and you will get to print final. What if you want to debug a function? You can use S to enter function blocks directly.
[email protected] ~]# python epdb2.py
>/root/epdb2.py (10)? ()
b = "BBB"
(PDB) n
>/root/epdb2.py (11)? ()
c = "CCC"
(PDB) n
>/root/epdb2.py (12)? ()
-final = Combine (A, B)
(PDB) s
--call--
>/root/epdb2.py (3) combine ()
def combine (S1,S2): # define subroutine Combine, which ...
(PDB) n
>/root/epdb2.py (4) combine ()
-S3 = S1 + s2 + S1 # Sandwiches S2 between copies of S1, ...
(PDB) List
1 Import PDB
2
3 def Combine (S1,S2): # define subroutine Combine, which ...
4-s3 = S1 + s2 + S1 # Sandwiches S2 between copies of S1, ...
5 s3 = ' "' + s3 + '" ' # encloses it in double quotes,...
6 return S3 # and returns it.
7
8 A = "AAA"
9 Pdb.set_trace ()
Ten B = "BBB"
c = "CCC"
(PDB) n
>/root/epdb2.py (5) combine ()
-S3 = ' "' + s3 + '" ' # encloses it in double quotes,...
(PDB) n
>/root/epdb2.py (6) combine ()
, return S3 # and returns it.
(PDB) n
--return--
>/root/epdb2.py (6) Combine () "AAABBBAAA"
, return S3 # and returns it.
(PDB) n
>/root/epdb2.py (13)? ()
Print final
(PDB)
If you do not want to step into the function, you can exit the breakpoint by pressing R directly to the place where you called it.
Dynamically change the value while debugging. Note that there is an error because B is already assigned, and if you want to change the assignment of B, you should use it! B
[email protected] ~]# python epdb2.py
>/root/epdb2.py (10)? ()
b = "BBB"
(Pdb) var = "1234"
(Pdb) b = "Avfe"
The specified object ' = ' Avfe ' is not a function
Or is not found along Sys.path.
(Pdb)!b= "AFDFD"
(PDB)
Post a Good article: http://onlamp.com/pub/a/python/2005/09/01/debugger.html?page=1
Debugger Module Contents
pdb
The module contains the debugger pdb
Containsone class, Pdb
which inherits from bdb.Bdb
. Thedebugger documentation mentions six functions, which create an interactivedebugging session:
Pdb.Run(Statement[,Globals[,Locals]]) Pdb. (expression[, globals [, Locals]]) Pdb.runcall (function[, argument , ...]) Pdb. () Pdb. (traceback) PDB pm ()
All six functions provide a slightly different mechanism for dropping a userinto the debugger.
pdb.run(statement[, globals[, locals]])
pdb.run()
Executes the string statement
under Thedebugger ' s control. Global and local dictionaries are optional parameters:
#!/usr/bin/env python import pdb def test_debugger(some_int): print "start some_int>>", some_int return_int = 10 / some_int print "end some_int>>", some_int return return_int if __name__ == "__main__": pdb.run("test_debugger(0)")
pdb.runeval(expression[,globals[, locals]])
pdb.runeval()
pdb.run()
is identical to, Exceptthat pdb.runeval()
returns the value of the evaluated string expression
:
#!/usr/bin/env python import pdb def test_debugger(some_int): print "start some_int>>", some_int return_int = 10 / some_int print "end some_int>>", some_int return return_int if __name__ == "__main__": pdb.runeval("test_debugger(0)")
pdb.runcall(function[,argument, ...])
pdb.runcall()
Calls the specified function
andpasses any specified arguments to it:
#!/usr/bin/env python import pdb def test_debugger(some_int): print "start some_int>>", some_int return_int = 10 / some_int print "end some_int>>", some_int return return_int if __name__ == "__main__": pdb.runcall(test_debugger, 0)
pdb.set_trace()
pdb.set_trace()
Drops the code into the debugger when executionhits it:
#!/usr/bin/env python import pdb def test_debugger(some_int): pdb.set_trace() print "start some_int>>", some_int return_int = 10 / some_int print "end some_int>>", some_int return return_int if __name__ == "__main__": test_debugger(0)
pdb.post_mortem(traceback)
pdb.post_mortem()
Performs postmortem debugging of thespecified traceback
:
#!/usr/bin/env python import pdb def test_debugger(some_int): print "start some_int>>", some_int return_int = 10 / some_int print "end some_int>>", some_int return return_int if __name__ == "__main__": try: test_debugger(0) except: import sys tb = sys.exc_info()[2] pdb.post_mortem(tb)
pdb.pm()
pdb.pm()
Performs postmortem debugging of the tracebackcontained in sys.last_traceback
:
#!/usr/bin/env python import pdb import sys def test_debugger(some_int): print "start some_int>>", some_int return_int = 10 / some_int print "end some_int>>", some_int return return_int def do_debugger(type, value, tb): pdb.pm() if __name__ == "__main__": sys.excepthook = do_debugger test_debugger(0)
"Python" Debug Tool-pdb (GO)