PyCharm Debug Tutorial | How to Debug Code in PyCharm!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody in this video i'm going to talk about the pie charm debugger the pycharm debugger is really helpful when it comes down to finding mistakes that you've made in your code and it becomes particularly useful when you have long blocks of code that you need to check for mistakes alright the first three things that i want to talk about are break points how to show the execution point and the step over function and in order to explain these three things let's have a look at the code that i've written in the editor at the moment you can see that i have six print statements and when i press the run button i have them printed out in the terminal so a breakpoint is pretty much the key constituent to any debugger and a breakpoint allows us to stop the execution of the program at a certain point in pycharm we can set a breakpoint by simply clicking in the margin and you can see a red dot appears and the line is highlighted in red now the next thing that i'm going to do is i'm going to click this debug button in the top right hand corner and you'll see that if i click it then the debug menu opens and when i click on the console over here you could see that not all the print statements are executed but instead only the first print statement is executed and that is precisely what the breakpoint has invoked it has interrupted the program after the very first print statement has been executed it is also possible to add more than one breakpoint if i go ahead and press on line six and add another breakpoint then proceed to resume the program you'll see that only the first three print statements are executed and then the execution stops at the break point on line six which means that the print statement in line six is not executed anymore another way to stop the execution of the program at a certain point is to put the cursor at the point where you want to stop the execution and subsequently click on run to cursor then you'll see that the execution stops at exactly the point where your cursor is placed so in this case if i put my cursor at the beginning of line seven then the print statement in line seven isn't executed anymore and only the first four print statements are executed now let's say that you move the cursor to another place on your editor and after you've done that you want to find the current execution point again the way to do that is to click on this sandwich symbol over here which says show execution point and as soon as you click it you'll see that the cursor will jump back to line 7 and show us the current execution put so if i click it you'll see that the cursor has now jumped back to line seven now let us have a look at what the step over button does we are going to set a break point only in line four and we're going to hit the re-run debug example one then we're going to go to the console and you'll see that only the first print statement has been printed if i now go ahead and press step over then you'll see that the next print statement is executed so the step over function does what it says it steps over to the next step in your program if i go ahead and press this a couple more times you will see that each individual print statement will be executed one after the other so the step over button simply goes to the next step in your program okay the next two things that we're going to look at are the step into and the step into my code buttons to demonstrate what these two buttons do i've prepared a small bit of sample code you can see that in the editor at the moment we have an import statement we're importing the module random and then we have a for loop and all this for loop does is print five random integers between zero and ten so if i go ahead and run this really fast you can see the console output gives us what we would expect which is five random integers between zero and 10. you'll also notice that i have set a breakpoint on line 6. now that breakpoint will become handy in just a moment but let's go ahead and now press the debug button after we press the debug button we're simply going to go down to the step into button and press that and let's see what happens you'll see that a new window has opened in the editor with a random.pi module in addition it has highlighted the return statement of the randomint function so all the step into button has done is that it has stepped into the function call so over here you can see that we have the random.random in function and once we pressed the step into function it has stepped into the call and gone into the random dot pi module and opened up the function randomint so now that we know what the step into button does let us now look at what the step into my code button does let's close the random.pi window and in the debug menu we are going to press the button step into my code once you'll see that now instead of opening the call to the function it has remained in the code that we have written so instead of going into the module it has remained with the code that is ours so the step into my code button does not open up function calls that lie outside of the code that we have written one interesting thing that you'll notice at this stage is that regardless of whether we press the step into my code button or the step over button we're going to have the same result so one question that emerges is what the differences between step over and step into my code to appreciate the difference between the two let us look at the following example code over here you can see that i have a function called addition which adds two numbers and a function called multiplication which multiplies two numbers further below i have two print statements the first print statement calculates two times three and then plus 4. so you can see over here is the multiplication it multiplies 2 and 3 together and then subsequently it takes that result and it adds 4 to it and in the print statement below we have the expression one times two plus three again you can see that we have the multiplication of one and two and subsequently we then go ahead and add three to the result so we're gonna go ahead and press debug and the execution will stop in line 12 where the breakpoint is set if i now go ahead and press step into my code you'll see that it will go into the first function call which is the multiplication function so it jumps over to the multiplication function and if i go ahead step into my code again you'll see it will then move on to the return statement so it moves through the code successively and it also moves into every function call which is in my written code okay now let's go ahead and rerun the debug and instead of pressing step into my code we're going to press step over once we do that you'll see the console logs the number 10 and what it's done differently compared to before is that it has not jumped into the function call it has stepped over the function call and simply moved to the next line of code and executed it now i do want to show you one more interesting thing at this stage let's go ahead and rerun the debug again and instead of pressing step into my code we're going to press step into and you'll notice that in the editor we have two purple boxes and when i press the arrow keys i can switch between these two boxes and what this allows me to do is it allows me to choose a function call that i want to step into let's say i want to step into the addition call i'm going to press enter on my keyboard and you'll see that it moves right into the addition function so i think at this stage it's starting to become clear that the buttons included in the debug menu allow us to navigate through our code in some way which we want to in order to find the bugs as fast as possible moving on i now want to show you the difference between step into and step out so up in the editor i have a function it is the addition function that we saw a moment ago and in addition to that i have a print statement which prints the addition of two and four if i now go ahead and run the debug it will stop at the breakpoint and now i am going to step into you'll notice that once i pressed it the line result a plus b is highlighted so the debugger went over into the function call and now if i press step out i am going to move back out of the function call to the print statement and if i press step over it will go ahead and execute the line and print the sum of two and four so as you can see the step into and the step out functions allow us to move into function calls and out of them again which makes the navigation through the code really simple next i want to talk about the evaluate expression button in the debug menu so in the editor at the moment i have a very simple program and all it does is it sums the numbers which are in a list if i go ahead and press run you can see that the program has simply summed the three numbers which are in our number list now if i go ahead and press debug the program stops at the breakpoint and over here in the debug menu you'll see the small calculator sign i'm going to go and press it and a small window pops up and it says expression so let's go ahead and type in some arbitrary expression let's say we would like to evaluate the sum of the first two numbers in the number list so we can go ahead and write the first number and then the second number remember when you are referring to an array the first number is always index zero and the second number has the index one so if i go ahead and evaluate that i get the sum of the first two digits of the numbers list now i can also go ahead and add the final number over here so numbers list index two and evaluate and i get the sum of all three digits just to give you another final example uh let me go ahead and add 12 to the result so we're going to have the result and we're going to add 12 then evaluate and you'll see that the result is 12 because at the moment the result over here has the value 0 so when we add 12 to it or 10 to it or whatever to it it is just the number over here so in summary all the evaluate expression window does is it allows us to evaluate some arbitrary expressions that we want to evaluate to help us debug some code the very last thing that i want to do is run you through a very easy example of how to debug a program so for that purpose i've created a very simple program which has a mistake in it you can see down here i have a print statement which should evaluate the expression five plus two times ten divided by two and if i go ahead and run this you'll see that it says that five plus two times ten divided by two is zero however that is not true it is meant to be thirty-five so somewhere in our program there seems to be a mistake so now let's go ahead and set a breakpoint at the print statement and run the debugger and once we've done that we can now go ahead and look into the individual function calls by pressing step into so now i can go ahead and step into one of the individual function calls let me go ahead and step into the addition function so you can see over here we have a is 5 and b is 2 which is just the way it is supposed to be so everything everything seems to be right over here and if i now go ahead and press step over you can see that the result of a and b is 7 and then it jumps back to our function so the addition seems to be correct because it's adding five and two correctly okay so let's go ahead and step into a different part of this function call let's go to the multiplication and over here we can see we have two numbers which is 7 and 10. so 7 is the number that arises when you add five and two so the first number seems to be correct the second number also seems to be correct now let's step over and you can see the result is 70 which is also correct so there doesn't seem to be a mistake in the multiplication either we can step over that as well and then we can go and step into the final function call now let's see what happens over here we have two numbers 70 and two and then when we move further by pressing step over you can see that the result of a divided by b is zero but that's wrong it's not meant to be zero and we can see over here we accidentally have a division we don't have a division symbol but instead we have a percentage sign so this is where the mistake lies let's go ahead and change that and now when we go ahead and press step over one final time we can now go ahead and rerun the debugger and let's move through the entire program by simply pressing step over and in the console output you will see that it says that the expression which we tried to evaluate before and which gave us the result 0 before is now 35. so in this very simple example the debugger has helped us find the mistake in this small easy example that might not be too relevant but when the programs get bigger this does become very very useful alright so that's it for this tutorial on how to use the debugger in pycharm if this helped you out then make sure to leave a like and subscribe and see you in the next video
Info
Channel: Code Bucket
Views: 1,444
Rating: undefined out of 5
Keywords:
Id: 76Lu6CfMuGg
Channel Id: undefined
Length: 14min 58sec (898 seconds)
Published: Fri Sep 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.