Summarizing the Python method with the PDB debugging

Source: Internet
Author: User
Tags function definition prepare

There are several ways to debug with PDB

The main way to debug Python programs with PDB is the following three kinds! The following describes

Command line plus-m parameter

The command line launches the target program, plus the-m argument, so that the breakpoint is called before the first line of the program executes testpdb.py

The next point of this article is the example show is to use this way to debug!

PYTHON-M PDB testpdb.py
Debugging in the Python interactive environment

>>> Import PDB
>>> Import testpdb
>>> Pdb.run (' testpdb.test () ')
Inserting a program into your code

More commonly used is to insert a program in the middle of the program, relative to a breakpoint in the general IDE and then start debug, but this way is hardcode

if __name__ = = "__main__":
A = 1
Import PDB
Pdb.set_trace ()
b = 2
c = A + b
Print (c)
Then run the script normally: Python testpdb.py to Pdb.set_trace () and it's settled, and you can see the debugger's prompt (PDB).

The debugging for this small program above is as follows:


Prepare test Program

Next, use the first method described above to debug a Python program to describe the commands that are commonly used in the PDB, but before you start, prepare the program code for the test:

testfun.py

This is a module that will be called by the main module, used to test the use of PDB debugging, it is possible to break from the main module tracking into the Sub module (follow-up instructions)

#!/usr/bin/python
#-*-Coding:utf-8-*-

def add (A, B):
Return a + b
testpdb.py

This is the code for the main module that was debugged below

#!/usr/bin/python
#-*-Coding:utf-8-*-

Def sub (A, b):
Return A-b

if __name__ = = "__main__":

print '
Import Testfun
i = 0
A = 1
while (I < 100):
A = Testfun.add (A, 1)
i = i + 1
print "Cumulative result:", a
Print ""

For letter in ' Pdb ':
Print "Current letters:", letter
Print ""

Fruits = [' banana ', ' apple ', ' Mango ']
For fruit in fruits:
Print "Current fruit:", fruit
Print ""


RET = 0
For NUM in range (10, 12):
RET = SUB (ret, num)
print ' Loop result: ', ret
Print ""

d = {' abc ': 123, 123: "ABC"}
for (K,V) in D.items ():
Print "Current key value pairs:", K, '-', V
Print ""
Summary of commonly used commands

Basic command

H (ELP) command: Print the current version of the PDB available commands, if you want to query a command, you can enter H [command], such as the H L View List command


L (IST) command: You can list the code blocks that are currently going to run


Breakpoint Management

B (reak): Setting breakpoints

For example, B 12 is to add a breakpoint to line 9th of the current script.

For example, B sub is to add a breakpoint to the sub function definition of the current script

In addition to being able to add breakpoints to the current script, you can also break breakpoints in the current script against other scripts, using the code above as example B testfun.add to add a breakpoint to the Add function in the testfun.py script

If you use only B, you will see all existing breakpoints


Condition Bpnumber [Condition]: Set conditional breakpoints, such as Condition 2 a==0, is the second breakpoint out of the conditional "a==0"


CL (EAR): Deletes a breakpoint, if followed by a parameter, is a clearly specified breakpoint, if not with the parameter is clear all the breakpoints


Disable/enable: Disabling/activating breakpoints


Program Logic Control

Here are a few commands that need to know the code and line number of the corresponding script, so here's a screenshot to show the first few lines of code to use in the following test


C (ont (inue)) to keep the program running until it encounters the next breakpoint


N (EXT), let the program run the next line, if the current statement has a function call, with n is not going into the body of the called function

As shown in the following illustration, when you debug a script breakpoint to Testfun.add (A, 1), continue to execute N and do not go inside the function of Testfun.add (A, 1)


S (TEP), similar to n, but if there is currently a function call, then S will go into the called function body

As shown in the following illustration, when debugging a script breakpoint to Testfun.add (A, 1), continuing to execute S will go into the Testfun.add (a, 1) corresponding function definition, although testfun.add is not a function defined in this script


J (UMP), let the program jump to the specified number of lines

If the current row is 10, note: If you execute the J 20, then the equivalent of the program to jump to 20 lines, the middle of the 11~19 line actually skip the past has not been executed, so if there is a variable in this code declaration or object initialization needs to be used in 20 lines and after, Then it can cause an error when you use it!


Printing Important Information

A (RGS), print the parameters of the current function. For example, the following image shows a breakpoint entering inside the testfun.add, printing testfun.add parameters


p, print a variable


Exit debugging

Q, exit directly from debugging; or use Ctrl+d to exit


One last word.

The process of using PDB debugging shown above is actually very simple, and the article mainly shows the effect of running by screenshots. If the simple look at the article, not an accident, there will be no clue, and even feel the screenshot of the command, output a mess, but if you go through the process in person, it will not take an hour, but the effect is absolutely excellent!

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.