Everyone knows that Python is a built-in Pdb library, and it is very convenient to debug Python programs using Pdb. However, Pdb cannot be used for remote debugging and multithreading. let's take a look at how to debug Python and common commands using the Pdb library. Everyone knows that Python is a built-in Pdb library, and it is very convenient to debug Python programs using Pdb. However, Pdb cannot be used for remote debugging and multithreading. let's take a look at how to debug Python and common commands using the Pdb library.
There are multiple ways to debug with Pdb
The following three methods are used to debug Python programs using Pdb! Next we will introduce them one by one
Add the-m parameter to the command line
Run the command line to start the target program and add the-m parameter. in this way, if testPdb. py is called, the breakpoint is before the first line of the program execution.
The example highlighted in the following section demonstrates how to use this method for debugging!
python -m pdb testPdb.py
Debugging in the python interaction environment
>>> import pdb>>> import testPdb>>> pdb.run('testPdb.test()')
Insert a program into the code
It is commonly used to insert a program in the middle of the program. compared to hitting a breakpoint in the general IDE and then starting debug, this method ishardcode
Of
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
Arrivedpdb.set_trace()
Then you can see the debugging prompt (Pdb ).
The debugging of the above small program is as follows:
Prepare the test program
Next, we will use the first method described above to debug the Python program and introduce the commonly used commands of pdb. However, we need to prepare the program code for testing before starting:
TestFun. py
This is a sub-module that will be called by the main module. it is used to test whether the sub-module can be traced from the main module to the sub-module at the breakpoint when Pdb is used for debugging (as described later)
#!/usr/bin/python# -*- coding: utf-8 -*-def add(a, b): return a + b
TestPdb. py
This is the code of the main module to be debugged below
#! /Usr/bin/python #-*-coding: UTF-8-*-def sub (a, B ): return a-bif _ 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 letter :", letter print "" fruits = ['bana', 'apple', 'Mango'] for fruit in fruits: print "current fruit :", fruit print "" ret = 0 for num in range (10, 12): ret = sub (ret, num) print 'cyclic result :', ret print "" d = {'ABC': 123,123: "abc"} for (k, v) in d. items (): print "current key-value pair:", k, '-', v print ""
Summarize common commands
Basic commands
H (elp) command: the available commands of the current version of Pdb are printed. to query a command, enterh [command]
For exampleh l
View list commands
L (ist) command: list the code blocks to be run.
Breakpoint management
B (reak): sets breakpoints
For example, B 12 adds a breakpoint to line 3 of the current script.
For example, B sub adds a breakpoint to the sub function definition of the current script.
In addition to adding breakpoints to the current script, you can also breakpoint other scripts in the current script. the code used above is used as an example.b testFun.add
You can add a breakpoint to the add function in the testFun. py script.
If only B is used, all existing breakpoints are displayed.
Condition bpnumber [condition]: set the condition breakpoint, for example, condition 2 a = 0, that is, add the condition "a = 0" at the second break point"
Cl (ear): deletes a breakpoint. if it is followed by a parameter, the specified breakpoint is clear. if it is not included, all breakpoints are cleared.
Disable/enable: disable/activate a breakpoint
Program Logic Control
The following commands need to know the code and line number of the corresponding script, so the first few lines of code to be used in the following test are displayed.
C (ont (inue), so that the program runs normally until the next breakpoint is encountered
N (ext), let the program run the next line. if the current statement has a function call, using n will not enter the called function body
As shown in, when the script breakpoint is debuggedtestFun.add(a, 1)
Continue to execute n and do not entertestFun.add(a, 1)
Function
S (tep), similar to n, but if there is a function call, s will enter the called function body
As shown in, when the script breakpoint is debugged testFun.add(a, 1)
Continue to execute s, will entertestFun.add(a, 1)
The corresponding function is defined internally, althoughtestFun.add
Not the function defined in this script
J (ump) to redirect the program to the specified number of rows
If the current row is 10, note: If j 20 is executed, it is equivalent that the program directly jumps to 20 rows, with 11 ~ In fact, the 19 rows are directly skipped and are not executed at all. Therefore, if the variable declaration or object initialization in this code needs to be used in 20 rows or later, when used, an error may be reported!
Print important information
A (rgs): prints the parameters of the current function. For example, when a breakpoint is displayed testFun.add
And print testFun.add
Parameters
P: print a variable.
Exit debugging
Q: Exit debugging directly. Alternatively, use Ctrl + D to exit.
Summary
The process of debugging with Pdb shown above is actually very simple. the article mainly shows the running effect. If you simply read the article, you will have no clue, and even feel that the command and output are messy. However, if you follow the process by yourself, it will take less than an hour, but the effect is absolutely good! To put it another way, the Python debugger is Pdb, which can be used to learn the C debugger gdb in Linux. the above is all the content in this article, hoping to help you learn and work.
For more information about how to use the Pdb library to debug Python and commonly used commands, see PHP!