Python debugging with PDB

Source: Internet
Author: User

Simple Debug Python Program 

You can also debug a program in Python like Gcc/gdb, as long as the PDB module is introduced when you run the Python program (assuming that the program you are debugging is named d.py):

$ VI d.py#!/usr/bin/pythondef Main ():        i, sum = 1, 0 for        I in xrange (+):                sum = sum + i        print sumif __name __ = = ' __main__ ':        Main () $ python-m pdb d.py

After running the above command, enter the following interface, you can enter GDB-like command to change the program execution process:

$ python-m pdb 1.py> d.py (3) () def main ():(PDB)

List shows the most recent code snippet for the program:

(Pdb) List  1  #!/usr/bin/python  2  3  ->def Main ():  4  i, sum = 1, 0  5 for  I In xrange:  6  sum = sum + i  7  print sum  8  9  If __name__ = = ' __main__ ':  Main () [EOF]

Next or N executes the next line of code:

(Pdb) next> d.py (9) (), if __name__ = = ' __main__ ':

Set a breakpoint on line 6th with break:

(PDB) Break D.py:6breakpoint 1 at D.py:6 (PDB) List  1  #!/usr/bin/python  2  3  def main ():  4  I, sum = 1, 0  5  ->for i in xrange (+):  6 bsum = sum + i  7  print sum  8  9  if __name __ = = ' __main__ ': ten  Main () [EOF]

If you want to set breakpoints at the function:

(PDB) Break D.maind.py:3 (PDB) List  1  #!/usr/bin/python  2  3 bdef Main ():  4  ->i, sum = 1, 0  5 for  i in xrange (+):  6  sum = sum + i  7  print sum  8  9  If __name__ = = ' __main__ ': Ten  Main () [EOF]

You can also add conditions to breakpoints, such as setting conditions only when sum > 50 breaks:

(Pdb) Break d.py:6, sum > 50Breakpoint 1 at d.py:6

If you want to see the value of a variable, you can print it with the PP command:

(PDB) step> d.py (5) Main (), for I in xrange (:(pdb) pp SUM0

The PDB module can be used directly in the program, after the import PDB pdb.set_trace ():

#!/usr/bin/pythonimport Pdbdef Main ():        i, sum = 1, 0 for        I in xrange (+):                sum = sum + i        pdb.set_trace () 
   print sumif __name__ = = ' __main__ ':        Main ()

This allows the program to run as long as it is running./d.py can be run directly to print sum:

$ ./d.py> d.py (9) Main (), print sum (Pdb)
Summarize

Command Use
Break or B Set breakpoints
Continue or C Continue to execute the program
List or L View the code snippet for the current row
Step or S Enter function
Return or R Executes the code until it returns from the current function
Exit or Q Abort and exit
Next or N Executes the next line
Pp Print the value of a variable
Help Help

Python debugging with PDB

Related Article

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.