Use the PDB library to debug Python programs

Source: Internet
Author: User
Python built-in PDB library, found to use PDB for debugging Program It is still very convenient. Of course, PDB may be confused about remote debugging and multithreading.

There are multiple ways to debug with PDB:

1. Run the command line to start the target program and add the-M parameter. In this way, if myscript. py is called, the breakpoint is before the execution of the program.
Python-m pdb myscript. py

2. Enable debugging in the python interaction environment
>>> Import PDB
>>> Import mymodule
>>> PDB. Run ('mymodule. Test ()')

3. The most common method is to insert a program in the middle of the program. Compared with the normal IDE, place a breakpoint and start Debug. However, this method is hardcode.

If _ name _ = "_ main __":
A = 1
Import PDB
PDB. set_trace ()
B = 2
C = a + B
Print (c)
Then run the script normally. When it reaches PDB. set_trace (), it will be fixed and you will be able to see the debugging prompt (PDB ).

Common Debugging commands

H (ELP) will print the available commands for the current version of PDB. If you want to query a command, you can enter H [command], for example, "H l"-view LIST Command
L (IST) to list the currently runningCodeBlock
(PDB) L
497 PDB. set_trace ()
498 base_data = {}
499 new_data = {}
500 try:
501 execfile (base_file_name, {}, base_data)
502-> execfile (new_file_name, {}, new_data)
503 TB:
504 logger. writelog ("error! Load result log error !")
505 print "load CMP logs Error !"
506 raise exception, "load CMP logs Error !"
507

B (reak), set a breakpoint, for example, "B 77", that is, place a breakpoint on line 77 of the current script, and enter the function name as a parameter. The breakpoint hits the specific function entry, if you only press B, all existing breakpoints are displayed.
(PDB) B 1, 504
Breakpoint 4 at/home/jchen/regression/regressionlogcmp. py: 504

Condition bpnumber [condition]: sets the condition breakpoint. The following statement adds the condition "A = 3" to 4th breakpoints"
(PDB) Condition 4 A = 3
(PDB) B
Num type disp ENB where
4 breakpoint keep Yes at/home/jchen/regression/regressionlogcmp. py: 504
Stop only if a = 3

CL (EAR). If it is followed by a parameter, the specified breakpoint is cleared (I have never succeeded on python2.4 !!!); If no parameter is specified, all breakpoints are cleared.
(PDB) Cl
Clear all breaks? Y

Disable/enable, disable/activate breakpoint
(PDB) Disable 3
(PDB) B
Num type disp ENB where
3 breakpoint keep no at/home/jchen/regression/regressionlogcmp. py: 505

N (EXT), let the program run the next line. If the current statement has a function call, using N will not enter the called function body

S (TEP), similar to N, but if there is a function call, s will enter the called function body
C (ONT (inue), so that the program runs normally until a breakpoint occurs.
J (UMP) to redirect the program to the specified number of rows
(PDB) J 1, 497
>/Home/jchen/regression/regressionlogcmp. py (497) comparelog ()
-> PDB. set_trace ()

A (RGS): prints the parameters of the current function.
(PDB)
_ Logger =
_ Base =./base/MRM-8137.log
_ New =./New/MRM-8137.log
_ Caseid = 5550001
_ Tostepnum = 10
_ Cmpmap = {'_ bcmpbinarylog': 'true',' _ bcmplog': 'true', '_ bcmpresp': 'true '}

P, one of the most useful commands to print a variable
(PDB) P _ new
U'./New/MRM-8137.log'

!, The exclamation mark is followed by a statement, which can directly change a variable.
Q (uit), exit debugging
It is also interesting to debug a program in the command line. record it and share it with you.

W, print a stack trace, with the most recent frame at the bottom. an arrow indicates the "current frame", which determines the context of most commands. 'bt 'is an alias for this command.

D, move the current frame one level down in the stack trace
(To a newer frame ).

U, move the current frame one level up in the stack trace
(To an older frame ).

Using the U and D commands, we can switch between stack frames to obtain the context variable information. W can display the latest stack frame information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.