Simple debug Python program using PDB under Linux

Source: Internet
Author: User

python comes with Debug ToolPak: PDB
#-*-Coding:utf-8-*-def func (num):    s = num *    return sif __name__ = = ' __main__ ':    print ' Debug starting ... '    print ' * ' *    print ' debug ending ... '    num = +    s = func (num)    print s
The PDB library is not referenced in a Python file, and you can add parameters when executing a python file:
python-m pdb demo.py
come to the PDB debug interface, debug mode will stop in the first line of the program code line

The following commands can be executed to perform the appropriate actions:
B (or break): Set breakpoint, set function: b demo.func; set line number: B demo:14 (number of rows)
N (or Next): Executes the next line:Step over, skip
C (or Continue): Executes the next breakpoint, without a breakpoint, to the end of the program: Step filter
s (or Step): Enter function,Step into, single-hop
R (or return): Jump out of function, Step return
L (or list): View the current code snippet
Q (or quit): Exits the current debug
p (or print): Print variable values
h (or help): View assistance
cl (Clear): Clears all nodes
disable [Bpnumber[bpnumber]]: Break the first num breakpoint, enable, fail 10th breakpoint
Enable [Bpnumber[bpnumber]]: Enables the first num breakpoint, enable 10, enables the 10th breakpoint
W (where): Print stack information
A (args): Print the current function parameter information


Of course, we can also use the PDB library function to implement the debug of the py:
For example: we can use the PDB function: Set_trace ()
def func (num):    s = num *    return sif __name__ = = ' __main__ ':    print ' Debug starting ... '    print ' * ' * 10
   pdb.set_trace ()          print ' Debug ending ... '    num =    s = func (num)    
Execute program: Python pdb_demo.py
This means setting a breakpoint line to the program, which will automatically run to the print ' py ending ... ' Stop and let the developer continue to trigger the next action, which sets the breakpoint just right when a py references another py.

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: [email protected] 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.