Python Tutorial for Beginners 48 - How to use PyCharm to debug Python code

 


hey guys welcome to the next blog on Python tutorial for beginners in the last blog we have seen how to debug our Python script using Python debugger or PDB now in this blog I'm going to show you how you can debug your python script using PyCharm ide so to start with I have this Python script which takes two user input both numbers and I have created this add a function which just add these two numbers and return the sum and using this print function I'm just printing the sum of these two numbers now let's run this code normally without any debugging and I'm going to just give two numbers here 44 and 55 and it's going to give me this kind of output which is wrong because the sum of 44 and 55 is not obviously 4 4 5 5 now some of you might already know the problem with this code but let's debug it with the help of the Python debugger so in order to start the debugging in pycharm IDE you just need to click this icon which looks like a bug and when you hover over it it will say debug so I'm going to click on this debug button which is going to start the debugging of my program so you can see here this debugging window is opened and this debugging window have two sections one is console section and other is debugger section so right now we are in the console section where we can provide the user inputs and see the output of our Python script just like the normal output now there is also a debugger window where you will be able to analyze the variables and all the frames of your program so I'm going to go to the console window and here I'm going to provide the first number and the second number and I'm going to press ENTER and it prints the sum of these two numbers which is not correct but our Python script is finished executing now in order to debug this Python script we need to set the break point at which the execution of the program will stop and we will go step by step after this point so I'm going to set the breakpoint at this line which is line number 13 at which my main function is called and then click on this line where you want to set the set point and you will be able to see this red circle logo which means that we have said the breakpoint at at this point so let's start the debugging once again and you will see because we have set the breakpoint here the program execution is stopped at the line number 13 and how you know that the execution is stopped at line number 13 you will be able to see under frames this line which is highlighted and you can see the line number is 13 so at this point the program execution is on the line number 13 also you will be able to see these kind of icon on top of this window so the first icon here is the step-over icon which you can click to step over your code the second icon is step into icon using which you can step inside your function or a class on which you are currently on the third icon is step into my code so generally if you use this option which is step into and your program contains some inbuilt or built-in functions then it's going to go into those built in function and this other option with say step into my code is going to only step inside your code and not into the built-in functions or the classes there are a few more I can share one is for step into and other a step out which we are going to see later also under this variables section you will be able to see the values of your variables so first of all I'm going to click this button which says step over you can also press f8 to step over so I'm going to click this button and because my program asks the user to provide some input I will go to the console and provide those input so I'm going to just give 33 as the first number and 55 as the second number and then once again I am going to press ENTER which is going to print the output and we come out of the debugging so now you know that this breakpoint is not helping us so we are going to remove this breakpoint and we are going to set our breakpoint here on the line number nine so here we will be able to see the values inside the variable x and y so that we know what's the problem inside our code so let me start the debugging once again and once again I'm going to give these two inputs 33 and 44 let's say and then you can see our program execution is stopped at this breakpoint and you will be able to see the values of your X variable and the Y variable now immediately you will see that this x value is the string and also this Y value is the string and whenever we use this plus operator with two strings it's going to give us the concatenation of those two strings also in the variables you will be able to see the list of variables and the type of those variables and the values inside those variables so you can see X has string type and the value is 33 and Y has also the typed string and the value is 44 now I told you that you can use this button to step into your code so I'm going to click on this button which says step into my code which is going to step inside this function so our program execution was on this line this was the breakpoint we have set and when I clicked the step into my code it has stepped into the function add if I would have pressed the step-over button then my program execution will go to the next line and not inside this add function so because I have pressed this button which say step into my code that's why it goes inside the function so here also you will be able to see the values of your X variable and Y variable and also you will be able to see the value of the sum when you just click the step-over button and here you will be able to see the value of sum which is three three four four which is the concatenation of these two numbers so immediately you will know that you are not adding these two numbers you are just concatenating two strings which gives you this kind of result so once you have pointed out the problem in your code we are going to stop our debugging so there is this red button here which is a square button I'm going to click on this which is going to stop that debugging now in order to fix this code I can just type cast this input which is a string into the integer and with the second variable also I am going to do the same and now let's start the debugging once again and once again I am going to give the two numbers 33 and 44 and press Enter and the program execution will stop at the breakpoint and now you will be able to see the values here which is 33 and 44 and under variables you will be able to see the type of these two numbers which is now in so both are int values I'm going to step over now so last time we have stepped into the function which is the add function this time I'm going to just press the step over button which is going to not go inside the add function but just tap over to the next line and you will be able to see the value of Z here which is 77 which is the correct sum of these two numbers so this is how you can use pycharm 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