Use the PDB library debugging program in Python and the pythonpdb library debugging

Source: Internet
Author: User

Use the PDB library debugging program in Python and the pythonpdb library debugging

The pdb library that comes with Python finds that it is very convenient to use pdb to debug the program. Of course, pdb cannot be used for remote debugging or 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.
Copy codeThe Code is as follows:
Python-m pdb myscript. py

2. Enable debugging in the Python interaction environment
Copy codeThe Code is as follows:
>>> 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.
Copy codeThe Code is as follows:
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 code blocks to be run
Copy codeThe Code is as follows:
(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.
Copy codeThe Code is as follows:
(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.
Copy codeThe Code is as follows:
(Pdb) cl
Clear all breaks? Y

Disable/enable, disable/activate breakpoint
Copy codeThe Code is as follows:
(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
Copy codeThe Code is as follows:
(Pdb) j 1, 497
>/Home/jchen/regression/regressionLogCMP. py (497) compareLog ()
-> Pdb. set_trace ()

A (rgs): prints the parameters of the current function.
Copy codeThe Code is as follows:
(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
Copy codeThe Code is as follows:
(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.