pdb - Python Debugger

  Using python programming will inevitably encounter bug, but pdb debugger python is a good language.

  The following describes the use of pdb

  1. stepping through the code, the command python -m pdb xxx.py startup script, enter single step mode

pdb command line:

  1) into the command line Debug mode, python -m pdb xxx.py

  2) h: (help) help

  3) w: (where) Print execution stack

  4) d: (down) in the current stack execution to jump to the deeper (personally I did not feel anything useful)

  5) u: (up) jumps to the implementation of the current layer stack

  6) b: (break) was added breakpoint

      b lists all current breakpoints, and to count the number of execution breakpoints

      b line_no: line_no the current line of the script to add a breakpoint

      b filename: line_no: script filename of line_no line to add breakpoint

      b function: add a breakpoint at the first executable statement of function function

  7) tbreak: (temporary break) temporary breakpoint

  After the first execution to the breakpoint, the breakpoint is automatically deleted, as usage and b

  8) cl: (clear) clear the breakpoint

      cl Clear all breakpoints

      cl bpnumber1 bpnumber2 ... clear the breakpoint number bpnumber1, bpnumber2 ... breakpoint

      cl lineno clear breakpoints in the current script line lineno

      cl filename: line_no clear breakpoints line_no line of script filename

  9) disable: disable breakpoint parameters bpnumber, and cl difference is that the breakpoint still exists, but is not enabled

  10) enable: activation breakpoints parameters bpnumber

  11) s: the (step) executes a command

    If this sentence is a function call, then s will perform the function of the first sentence

  12) n: the (next) one statement

    If this sentence is a function call, the function is executed, then a currently executing the next statement.

  13) r: (return) to the end of the execution of the current operating function

  14) c: (continue) continue to execute until the next breakpoint

  15) l: (list) Source List

      l lists the currently executing statement around 11 Code

      L around the first listed first line of code 11

      l first second listed first - second range of codes, the number of lines if the second <first, second will be interpreted as

  16) a: (args) lists the function of the current execution of the function

  17) p expression: (print) the output value of expression

  18) pp expression: a little good-looking p expression

  19) run: Restart debug, equivalent to restart

  20) q: (quit) quit debug

  21) j lineno: (jump) set statement function performed under Article

      Only at the very bottom of the stack to jump backwards again executed, can be executed directly to the forward line number

  22) unt: (until) to the next line (out of the loop), or the end of the current stack

  23) condition bpnumber conditon, to set a breakpoint condition, condition returns True if the argument when bpnumber breakpoint is valid, otherwise invalid breakpoint bpnumber

 

note:

  1: direct input Enter, will execute the previous command;

  2: Enter the command did not know PDB, PDB take him as a Python statement is executed in the current environment;

 

Example:

# test1.py

s = '0'
n = int(s)
print(10/n)

Open a command line to run test1.py

python -m pdb test1.py

pdm positioned to the code to be executed next -> s = '0', the input command l, which is above the first command 15 l (list) to see the Code:

N input commands can step through the code

You can also enter the variable name to see the variables p

But the code where this variable must be after running over to view, otherwise, the situation will not find variables, as follows

Beginning operation test.py, the first line s = '0', this line has not been actually performed. At this view you will be prompted to find the variable variable s

 

Enter the command q end the debug to exit the program

 

   2. pdb single step too much trouble, so after the second method is to import pdb, directly in code debugging needs a place to put pdb.set_trace (), you can set a breakpoint, the program will pdb.set_trace ( ) pause and enter pdb debugging environment, you can use pdb variable name to view a variable, or c continue

Examples of the above modifications are as follows, import pdb, added pdb.set_trace () to the error code may previously

# test1.py

import pdb


= S '0'
n-= int (S)
pdb.set_trace () Run # here will automatically pause
print (10 / n)

After running the program to the next line of code breakpoint on the suspension

 

Guess you like

Origin www.cnblogs.com/chester-cs/p/11580331.html