Python pdb Tutorial - Python pdbIn this tutorial, we will learn about the Python debugging tool with pdb. Debugging applications are the unwelcomed activity when developer is being busy working under a time crunch and he/she want it to work. We will show the basics of the using pdb, Python's interactive source code debugger. We will also walk through the some common use of pdb. It is a good practice to tracking down the bugs and allows fixing faulty code more quickly and reliably. Let's have an introduction to Python pdb module. Python pdb ModulePython pdb module is used to do the debugging in the Python which comes built-in to the Python standard library. The pdb is defined as class Pdb which internally makes use of bdb (basic debugger functions) and cmd (support for line-oriented command interpreters) modules. It provides the advantage of running on the command line thereby making it great for debugging code on remote server. The pdb module supports the setting breakpoints, stepping through code, viewing stack traces, and source code listing. Starting Python DebuggerPython provides the many ways to invoke a debugger. We need to insert import pdb, pdb.set_trace() commands to start debugging within the program. If we define the breakpoint in the program, our script will stop its execution on that breakpoint automatically. Python provides the breakpoint() function which works in the same as we define manually. Let's understand the following example. Example - 1: Adding of two numbersIntentional Error - The input() function returns the string program concatenates those strings instead of adding input numbers. Output: > d:\python project\test_pdb.py(8) Explanation - In the above output, first line is a directory path of the executable file, line number where our breaking point is located, and <module>. It means that we have a breakpoint in test_pdd.py file on line number 8 it is appearing inside <>. The next line is indicating the code line where our execution is stopped. That line is not executed yet. Then we have the pdb prompt which can be used to navigate the code using the below command.
For example, when we run the help command in pdb prompt it shows - Now we can check the type of the variable whatis and variable name. Let's see the following example. Example - When we use the next command - Printing ExpressionThe print command evaluates the passing expression. If we pass a variable name, pdb prints its current value. However, we can perform several operations to identify the state of our running application. Let's understand the following example. We get the file path using the get_path() method. To inspect the program process, we call pdb.set_trace() to pause the execution. Example - Output: > d:\python project\test_pdb.py(891)get_path() -> return head (Pdb) So what is happening in above code -
Now let's print some expression to get the current state of the application. We can pass the valid evaluation expression to p command. These expressions are very helpful when we are debugging the program and want to use the test alternative implementation directly. The pp (pretty-print) command can also be used. It is useful when we want to print a variable with a large amount of output. Post-mortem DebuggingThe post-mortem debugging refers to entering debug mode after the program is finished with the execution process. The pdb module provides the pm() and post_morterm() functions. This function directly targets the line which causes to the error. Let's understand the below example - Example - Output: Enter first number : 34 Enter second number : 65 Traceback (most recent call last): File "d:/Python Project/test_pdb.py", line 9, in Checking Variable StackAll the variables used by the program local or global are maintained on the stack. We can use the args(or use a) to print all the argument of the function which is currently active. We can use the p command to evaluate an expression given as an argument and prints the result. Let's see the following debugging. Example - Displaying ExpressionThe pdb module provides other commands apart from p and pp to tell pdb to display the value of an expression, if it changed, when execution stops. Let's understand the below syntax and description of commands.
Python Pdb BreakpointThe breakpoints play essential role when we work with the large program. We often want to add the number of breakpoints where we know error might occur. The pdb module provides the break command to do so. Let's see the following syntax. Syntax - If filename is not given, the line number lineno, then the current source file is used. The second argument condition is very powerful and important. Using this, we can apply the break point only if condition existed. If we pass the second argument, pdb will break when the expression evaluates true. Let's understand the following example - Example - Output: > d:\python project\test_pdb.py(7) Managing BreakpointsWe can also manage the breakpoints using the enable, disable and remove command. The disable allows debugger not to stop when the breakpoint is reached while enable activates the disable breakpoints. Let's understand the following example - Example - Output: > d:\python project\test_pdb.py(7) ConclusionIn this tutorial, we have covered basic and common concepts of the pdb module. We have learned printing expression, pdb commands, how to use breakpoints, displaying expressions and implemented some example. Debugging helps the programmer to analyze program line by line. Python comes with the default debugger that is easy to import and use. The pdb module play essential role in debugging especially when we work with the large program. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India