Python Tutorial for Beginners 47 - An Introduction to Python Debugger (pdb)

 


hey guys welcome to the next blog on python tutorial for beginners in this blog we will talk about a command-line tool which is called PDB which we used to debug our Python script now PD b stands for python debugger and it is already installed whenever you install python on your operating system so you don't need to install a PDB separately if you have Python installed it will work on your terminal now you might wonder why I'm using PDB if I already have the PyCharm IDE and I can debug my code on the PyCharm IDE now in real-life situations most probably you will run your python script on some kind of a server now generally on these servers you don't have the luxury of using an IDE you will have to use the terminal in order to debug your Python script and that's why I wanted to show you how to debug your python script using the command-line tool which is PDB now to start with I have this simple Python script which asks the user to input two numbers and these numbers will be saved in these two variables which is x and y now we are using a function called add to add these two values and then we are just transferring the result into this Z variable and we are just printing the value of Z now in order to open the terminal in the pycharm IDE you just need to click on this option which says terminal and if you don't want to use this terminal you can also use a Windows command prompt or Linux terminal or if you are working on Mac OS you can also work on the Mac terminal so what you need to do is you just need to copy the part of your file you are working and then you just need to CD to the directory in which you are working in so I'm going to just CD to the directory in which my Python script is there and then the command will be same to run and use PDB on your terminal so you can use the Windows command prompt or this terminal which is available in the PyCharm or any other terminal which is available on your operating system so I'm going to use this terminal which is available in the PyCharm now I'm going to move this terminal to the right hand side so we will be able to see the code and the command terminal at the same time now in order to run your script you already know that you just need to give this command which is Python and the name of your script in my case the name of my script is debugging dot py so I'm going to just press ENTER and then it's asked me to provide the first number I'm going to provide the first number and then I'm going to provide the second number which is going to give me the sum of these two numbers so here it prints three three four four which is not the addition of these two numbers now some of you might already know what is the problem with this code and how to solve it but we will use the debugger to find out what is the problem and how to solve this problem so that whenever we enter two numbers it's going to give us the proper sum of these two numbers and not the other answer now in order to start the debugging on your Python script you can provide this command so you can give Python - M PDB and the name of your script so my script name is debugging dot py and then I'm going to press ENTER and now you will be able to see this kind of interface and you will be able to see PDB printed here and there is a cursor blinking here and this Python debugger which is Python PDB starts from the first line of code which you have on your script so we are on this line which is this line in our script which is that declaration of this ad function now the first thing you need to know in order to work with this PDB interface is the help command so you can give this help command here or you can give the H key word which means that help command so I'm going to give the full help keyword here and then press ENTER which is going to give us the list of each and every command which you can use with the PDB so these are all the commands which you can use with PDB the most important command which we will be using frequently is the next command or shortcut of the next is n or we will be using the step command or the S keyword for the step command otherwise we will be using the C key word or the continue command these are the three most frequently used command to work with the PDB now you can see you can type help and the name of the command in order to know more about that command so I'm going to just write help here and the name of the command let's say I want to just know more about the next command so the next command continue execution until the next line in the current function is reached or it returns so in this way you can get the help about each and every command which you have here now once again we are going to find out at which position PDB is right now we already know that PDB is on this line but there is a command called where or you can also write W to find out where right now your PDB is so I'm going to just write where it's going to show on which line your PDB is currently standing so now I'm going to give the end keyword you can also give the next command here and I'm going to press ENTER and it's going to go to the next line which is this line is that if name is equal to main so now we are on this line right once again when I press ENTER it's going to execute the last executed command so we have executed last this next command so if you press ENTER directly without entering any command it's going to execute the last executed command so now we are on this line which is this one which takes the input from the user and you will also be able to see on which line this code is so this line is at the seventh line now once again I'm going to press help here and then press ENTER and now I want to use this continue command so this continue command is used to continue the execution of your script from the point where you are right now so we are right now at this line which is line number seven and if I press C or if I type continue which is this keyword and I press ENTER then my program is going to execute and it will ask me the first number so I'm going to provide the first number let's say two and then I'm going to provide the second number which is 3 and then when I press ENTER it's going to execute the program and it's going to print the result which is 23 in this case which is wrong and after the script is finished the PDB is going to restart the execution from the top so you can see this line the program is finished and will be restarted so once your program is finished the PDB is going to restart it from the starting point from where it has started so I'm going to quickly provide the N command for next and once again next and I'm going to once again press next and provide the number which is 3 this time so now we are on this line which is the line number 8 which is this line so the execution of this line is finished now there is a print command in PDB so you can just try it print and the name of the variable so I'm going to just write X here because this line is already finished and it's going to give me the value of X so when I press ENTER it's going to print 3 which is the value of X once again I'm going to press the end command or the next command and then I'm going to provide the second number which is 4 this time and I'm going to print the value of the Y now so till now nothing seems to be wrong with this code so let me just press H for the help once again and we are going to see the list of commands once again and now I want to use this command which is what is so I'm going to just write what is and the name of the variable which is X okay so earlier we have printed the value of x which is 3 and now I'm writing what is X so we want to know what type of value is inside X so I'm going to just press ENTER now and once you do that you will see that X is an instance of class string and we wanted to enter the number right so now we know what is the problem so because input function is going to return the string value and this add function is just one catenate these two string values and it's not adding the two numbers so what is command is going to give you the information about the variable or the function but we will continue our debugging so you can see here right now we are on this line which is this line so now I want to go inside the add function in order to inspect if it's working fine or not so we are on this line where add function is called so I can use this command which is the step command or I can use the shortcut which is s in order to step into the function where we are right now so I'm going to just type step here and then enter and you will see we are here on the Declaration of ad function once again and if we type N or next once again we will go to the next line of this function which is this line which we use to add the value of x and y so this command step or this letter s can be used to step into your function or step into your class if you are using class it's going to step into or inside that function so now I'm going to just press and to go to the next line and this is the return some line and now we already know what's the problem so we can press C or continue in order to continue the execution of this program so I'm going to press C and then press ENTER which is going to continue the execution of the program and it has printed the result and also it has restarted the PDB once again from the starting of the script so now let's correct this code and in order to convert the input into int or the float you can just type cast the string to int using this int function so I'm going to just use this in function in front of both the input values so now we know that our script is most probably going to run fine so let's try to set the breakpoint at this line because both the values will be available at this point and we will know the values of x and y at this point so we will be able to see the type of both the values so first of all what is a breakpoint so when you set a breakpoint at some line the program is going to stop exactly at this line so generally we have seen that if you start the PDB it's going to start from the beginning of your script but if you set the breakpoint the execution will only stop at the line where you have set the breakpoint so once again I'm going to press H to get the help and I will see which command can I use to set the breakpoint so there is this command called break which I can use to set the breakpoint so let me get the help about the break it's going to give us the description about the break command so you can use the break keyword or the B letter in order to set the break point so you can see it takes these arguments so in order to set the break point you just need to write break and then the line number on which you want to set the break point so let's say I want to set the break point on the line number nine so I can just write break and then nine and then press Enter which is going to set the breakpoint on line number nine now I can give the Kuantan new command in order to continue the execution of the script and now when I press continue it's going to first ask for these two values so I'm going to quickly give the value of first number and the second number and then when I press ENTER it's going to stop at this line where we have set the break point right so right now we are on this line which is line number nine and now I can give this command which is what is X and once again it says that the type of this X is string so what might be the problem the problem is after changing our script we haven't restarted the PDB we haven't exited the PDB and restarted the PDB so this script is not loaded after we have edited the code right so in order to quit the PDB you give the quit command or the cue letter so just press Q and then press Enter which will allow you to come out of PDB and let me run that debugging once again so you can once again give this command Python - am pdb debugging dot P Y and then press Enter which is going to restart the debugging and now this script is loaded once again with the correct code which is IND so now let me just put the breakpoint on the line number ten which is this line and now I'm going to press see to continue and I'm going to give these two numbers let's say and you can see the program is halted on the line on which we have set the breakpoint on so once again I'm going to give the what is command and the name of the variable which is X and then press Enter and now you will see the type of X is now int and we have solved the problem so we can simply press C to continue and see the result and you can see it prints the result which is 8 which is the addition of 3 and 5 so our debugging was successful and we are now seeing the expected result which we wanted to see so this is how you can use PDB to debug your Python script now there are a few more ways of using PDB with your Python script so let's see those other ways in which we can use the command PDB with our Python script so I'm going to press Q in order to exit out of the debugging so the second way of using PDB is to import the PDB inside your script so I can just import the PDB library inside our script and then in order to set the breakpoint I can use PDB dot set trace so this will set the breakpoint on the next line after this line so I'm going to just clear the terminal by right clicking and then pressing clear buffer and once again to start the debugging I just need to give this Python command and the name of my script so once you have this import inside your script and you have this code inside your script you don't need to give the PDB command you just need to run your script normally with the Python command so my program execution is here which is running the program normally so I'm going to just scrape the value of x and y which is 3 and 4 and it's going to stop the execution at this line which is just after the set trace code ok so because we have set the breakpoint on this line using this line of code it's going to stop at this point and once again I'm going to use for example what is command and I want to see the value of X and it's going to show us that type of X which is in so this is the other way of using pdb in your script some people also like to use PDB directly at the point where they want to set the breakpoint and the benefit of using this type of code is if you are using any linting tool it's going to warn you on the same line so the developer will know that he needs to remove this line because linting will fail because there is a semicolon here and we are using two line of code in the same line so it's going to give us the warning and that's how the developer will know that he needs to remove this line so he is going to remove this line after the script is working fine so this was the second way I'm going to quit out of the PDB once again and let me show you the last way of debugging your code with the PDB command so I'm going to clear the terminal and now I'm going to just give this Python command which is going to open the Python shell and here you just need to import the script which you are working with so I'm working with the debugging script and then import the PDB and once you have imported your script and the PDB module you can just use PDB in order to start the debugging of your script so PDB dot run and under the brackets or under these parentheses you just need to give the name of your script so my script name is debugging and then you need to provide at which point you need to start the debugging so let me enclose this code inside a new function so let me define a new function and I'm going to name it as mean and this is going to be my main function and inside the main function I'm going to execute all this script and I'm going to just call this main function inside this if condition and here on the Python shell I can just write debugging dot the function which I want to call so I'm going to just call the main function and then I'm going to press ENTER and once again it shows me that debugging doesn't have this main method because after saving I haven't restarted the PDB once again so let me just quit once again quickly and let me just quickly close this terminal and restart this terminal and once again I'm going to give the Python command which is going to start the Python shell and I'm going to import the PDB and the script once again and now I can just give the PDB dot run command and then give the name of the script on which I want to work on which is debugging and then I'm going to call the mean method inside the debugging and then press ENTER and once again give the N command which is for the next which is going to start the debugging of your code so this is how you can use the PDB command in order to debug your Python script I hope you have enjoyed this blog and I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News