How to use the PDB for Python debugging

Source: Internet
Author: User
Tags pprint

Excerpt from: http://www.91linux.com/html/article/program/python/20111119/22356.html

This article explains how to use the PDB for Python debugging.
When at hand

Without the IDE, you can refer to this article if you are worried about Python debugging. (PDB command Debugging)
Reference: http://docs.python.org/library/pdb.html
and (PDB) help
Using the PDB for Python debugging, the usage is basically the same as GDB,
Let's look at a simple example:

epdb1.py. # epdb1.py--Experiment with the Python debugger, PDB " AAA "  "bbb" "CCC"= a + B + C  Print (final)


For example, to debug this program:
1: Add this sentence to the front of the file and introduce a debug module.
Import PDB
2: On the line to start debugging Add the Pdb.set_trace () file to:

# epdb1.py--Experiment with the Python debugger, PDB Import " aaa " "BBB" " CCC "  = a + B + cprint final


Can run this program, to the breakpoint out will stop, similar to GDB,
You can execute the command:
Direct enter is to repeat the previous command!
P (print) view a variable value
N (Next) Next
S (step) Single step, enter function
C (continue) keep moving.
L (list) View source code



There are several ways to debug with PDB:
1. Command line to start the target program, plus the-m parameter, so that the call myscript.py Word breakpoint is the execution of the first line of the program
PYTHON-M PDB myscript.py
2. Enabling debugging in a python interactive environment
>>> Import PDB
>>> Import MyModule
>>> Pdb.run (' mymodule.test () ')
3. More commonly used is to insert a program in the middle of the program, relative to the general IDE inside the breakpoint and then start Debug, but this way is hardcode if __name__ = = "__main__":

A = 1= 2= a + bprint(c)


Then run the script normally, to Pdb.set_trace () then it will be fixed and you will see the debug prompt (Pdb).
Common debug Command H (ELP), which prints the commands available for the current version of the PDB, and if you want to query a command, you can enter H [command], for example: "H L"-View the List command
L (IST) to list the blocks of code that are currently going to be run
(PDB) L
497 Pdb.set_trace ()
498 Base_data = {}
499 new_data = {}
Try:
501 execfile (base_file_name,{},base_data)
502-execfile (new_file_name,{},new_data)
503 except:
504 Logger.writelog ("error! Load result log error! ")
505 print "Load CMP logs error!"
506 Raise Exception, "load CMP logs error!"
507> Breakpoint Settings
(Pdb) B10 #断点设置在本py的第10行
or (Pdb) bots.py:20 #断点设置到 ots.py Line 20th
Delete Breakpoint (Pdb) b #查看断点编号
(PDB) CL 2 #删除第2个断点
> Operation
(PDB) n #单步运行
(PDB) s #细点运行 that's going down, method
(PDB) C #跳到下个断点
> View
(Pdb) p param #查看当前 Variable value
(PDB) L #查看运行到某处代码
(PDB) a #查看全部栈内变量 B (reak), set breakpoints, such as "B 77″, that is, the current script in the 77 lines of the breakpoint, but also enter the function name as a parameter, the breakpoint hit the specific function entrance, if only knock B, will show all the existing breakpoints
(PDB) B 504
Breakpoint 4 at/home/jchen/regression/regressionlogcmp.py:504 condition Bpnumber [condition], set conditional breakpoints, The following statement adds the condition "a==3" to the 4th breakpoint.
(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 followed by a parameter, clears the specified breakpoint (I have never succeeded on the Python2.4!!!); All breakpoints are cleared if no parameters are taken.
(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, N is not entered in the body of the called function.
S (TEP), similar to n, but if there is currently a function call, then s will enter the body of the called function
C (ont (inue)) to allow the program to function until a breakpoint is encountered
J (UMP), let the program jump to the specified number of rows
(PDB) J 497
>/home/jchen/regression/regressionlogcmp.py (497) com parelog ()
-Pdb.set_trace () A (RGS), printing the parameters of the current function
(PDB) A
_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 '!, after the exclamation point followed by the statement, you can directly change a variable
Q (uit), exit debug

==============================================================================================
Use the PDB module in Python to debug
Import PDB
Pdb.set_trace ()

You can also use the python-m pdb mysqcript.py this way

(PDB) will automatically stop in the first line, waiting for debugging, you can see the Help
(Pdb) H
Explain these key commands

> Breakpoint Settings
(Pdb) B #断点设置在本py的第10行
or (Pdb) b ots.py:20 #断点设置到 ots.py line 20th
Delete Breakpoint (Pdb) b #查看断点编号
(PDB) CL 2 #删除第2个断点

> Operation
(PDB) n #单步运行
(PDB) s #细点运行 that's going down, method
(PDB) C #跳到下个断点
> View
(Pdb) p param #查看当前 Variable value
(PDB) L #查看运行到某处代码
(PDB) A #查看全部栈内变量
(PDB) W lists the layers in the current call stack.
(Pdb) d moves down one layer in the call stack
(Pdb) u move up one layer in the call stack. If you press n after a layer has been moved up, the next narrative will be performed at the level after the move up, and the previous function call will automatically return.
(PDB) CL clears the specified breakpoint. If no parameters are taken, all breakpoints are cleared.
(Pdb) Disable the ability to cancel all breakpoints, but still retain those breakpoints.
(PDB) enable the ability to restore breakpoints.
(PDB) Ignore sets the number of times the breakpoint is ignored. If count is not specified, it is initially 0. When Count is 0 o'clock, the breakpoint will act normally. If count is specified, the count is less than 1, until the count is 0, each time the interrupt is performed.
(Pdb) condition Bpnumber [condition]
(PDB) J (UMP) Lineno. Jumps to a row to execute. It only works at the bottom of the call stack.
(PDB) L lists the location in the current file. The continuous l command is always listed at the end of the file and can be printed using the specified number of rows or ranges.
(Pdb) the PP and P commands are similar, but using the Pprint module (Pprint is not used, please refer to Python Library Reference for details).
(PDB) alias takes an "alias" instead of "a bunch of debug commands," Something like C/S macro (see Python Library Reference for details).
(PDB) Unalias cancels an alias.
(PDB) [!] Statement the narrative in the current environment (context).

How to use the PDB for Python debugging

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.