00:00 All right! In this video, we’re going to look into debugging with PyCharm—one thing that this IDE is especially great at. You’ll see that it makes debugging much simpler and more straightforward than other text editors. Over here with our code, we have a functioning one at the moment. We can run it and it produces the output we want.
00:28 What you can do in PyCharm is you can simply set up a breakpoint by clicking here in between the numbers and your code. Click here, and it creates this red dot, which means that it sets a breakpoint. You can also see at the bottom, I have this switched on where it tells you what’s happening.
If I remove this, we’re not updating
x, but we’re still printing
x. So something’s wrong there. And if I run the code, you see, we’re not getting the output that we would expect. So let’s debug what’s going on here using the debugging tools in PyCharm.
01:33 And the great stuff that’s happening, for example, is that we can right away see what are the variables that are declared in here. You can walk down the stack file—we’re not going to do that right now, but I want to show you that this gives us a great way of having an overview of what’s happening, okay? So I can step forward in the program—just use this one.
We also have step into and all the things that you’re used to from using a debugger. I’m just going to step over to the next line. We see
0, and PyCharm even writes this right where it is being defined.
x is being defined in here, and the current value is
0. If I keep moving forward, this is going to change. So now you see, because we’re in the next iteration of the loop, it tells us
1 now. And down here we can also see that
1, and we even see what type it is—this is an integer, right?
We keep going. And
2. We can already notice that this is not being applied, like
x doesn’t change.
x is just the normal numbers that it goes through in the loop, and that’s what’s getting printed out.
So here we have our problem, right? Now I want to show you some other cool stuff that it can do, however. Because it’s interactive, right? So you can press this calculator symbol in here where we currently have this variable
x defined, and this opens up the possibility to evaluate an expression. So for example, we have
x here. If I press Enter it’s going to tell me that
x refers to an integer that has the value
I can do stuff in here, like I can say, “What should it actually be?” I want to square it? Yeah, so what I would actually want is the integer number
4. I can work with the variables that are currently defined in there.
So this, especially in bigger programs, this is great to take a look—imagine you’re web scraping something, and there’s this
BeautifulSoup object you have to work with, for example, and then you can just hop in here and then you can simply search in there.
03:44 Is the thing that you’re looking for on this website even actually—are you gathering it? Is it in there? And you can simply stop your program with using these breakpoints and go into interactive mode, and just work with the variables at the current state of the program. You can set multiple breakpoints, you can jump between them, and this is just a very powerful tool that helps you to figure out what’s going on in your program and fix those errors quickly.
There’s all these hints in there that help immensely with debugging as well. Without even needing to run the debugger, it gives you hints. This one is highlighted in yellow and if I hover over it, it tells me this Statement seems to have no effect. Because yes, we’re squaring
x, but we’re not doing anything with it.
04:36 We’re not assigning it anywhere, so it just happens and it’s lost. In that case, this would already be enough for us to figure out that, “All right, okay, we have to assign this to something and then it’s going to do what we’re actually wanting it to do.” All right, so that’s a quick introduction of working with the debugger.
04:53 The two most important things to remember about it are set your breakpoints, and then press the bug to start it. Well, that’s just the setup. And then there’s two things—you can see the defined variables in here, so you have a variable inspector. You can step over your code or step into something if you have a function somewhere.
05:25 Explore a little around at any specific point where you’re stopping your program. All right! So that’s a very powerful feature in PyCharm. There’s lots more to learn about debugging if you want, there’s great tutorials online also from PyCharm directly.
Become a Member to join the conversation.