Python pdb Tutorial - Python pdb29 Aug 2024 | 7 min read In 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. |
While searching for monitoring the infrastructure or third-party applications, the built-in plugins of Telegraph become a great choice for us. Or we are searching at system resources such as disk and network utilization or the performance of the MySQL database. What if we are creating an application,...
9 min read
Introduction The is used to return a value from a function. The user can only use the return statement in a function. It cannot be used outside of the Python function. A return statement includes the return keyword and the value that will be returned after...
3 min read
Basics of vowel and Consonants In the English language, there are 26 alphabets, out of which a, e, I, o, and u have considered vowels, and the rest have considered consonants. In the article, we will use different methods to check whether the given character is a vowel...
11 min read
In the following tutorial, we will learn about the webbrowser module in the Python programming language with some examples. So, let's get started. Understanding the Python webbrowser module The webbrowser module is a convenient web browser controller in the Python programming language. This module offers a high-level interface that...
3 min read
? One of the most well-liked and adaptable programming languages, Python may be used to create a variety of applications. Python gives programmers the ability to create server-side or backend code for web applications. Several frameworks and packages are also included. Considering this, we will attempt to...
3 min read
A tree-based information structure called a "Trie" is portrayed as being utilized to store assortments of strings and complete speedy pursuits on them. The name "Trie" comes from the action word "Recovery," which indicates the demonstration of finding or securing something. Two strings will have a similar...
19 min read
- Element Tree Library In this tutorial, we will learn how we can parse XML using Python, modifying and populating XML files with the Python ElementTree package. To understand the data, we will also learn about the XPath expression and XML trees. Let's have a brief introduction...
13 min read
Django is a popular web framework that is built on top of the Python programming language. It provides developers with a powerful toolset that enables them to rapidly build web applications that are robust, scalable, and secure. One of the key features of Django is its...
3 min read
In this tutorial, we will learn about the Google's protobuf and how we can implement using Python programming language. Suppose, there is a group number of people of different origin and they speak different languages. To communicate effectively, they try to use a language that everyone can...
8 min read
NumPy is a powerful library in Python that offers a guide for operating with big, multi-dimensional arrays and matrices of statistics at the side of a group of mathematical functions to operate on those arrays. One such feature that is available in handy while operating with...
4 min read
We request you to subscribe our newsletter for upcoming updates.
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