Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Debugging

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:19 So, what happens in there? We don’t really see it. So, imagine this is a bit more complex one, and you want to figure out what’s going on somewhere in the middle.

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.

00:43 It’s toggling—like, either creating or removing a breakpoint.

00:48 We see the whole line is in this nice red, so something’s going on here, right? If I run my program normally, nothing happens—as it should with a breakpoint.

00:58 But now, if I want to debug—I want to figure out what’s going on here—then I can press the debug one. Let’s actually introduce a bug in here.

01:07 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:23 Instead of pressing the green triangle to run it, I’m going to press the bug symbol. And you see, PyCharm opens up a Debugger window down here.

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.

01:51 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 x equals 0, and PyCharm even writes this right where it is being defined.

02:04 So 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 x is 1 now. And down here we can also see that x is 1, and we even see what type it is—this is an integer, right?

02:22 We keep going. And x stays 1, x is 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.

02:37 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 2.

03:01 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.

03:13 If I continue this, so x equals 2let’s move forward, x equals 3.

03:21 So if I evaluate it now, I get the current value that’s in my program. So it says x to the power of 2 now—it’s going to be 9 because x refers to the value 3.

03:32 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.

04:09 Now, PyCharm does even more than that. In this case, if you remove the breakpoint and stop the debugger, you can see it’s already telling us something here.

04:19 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:16 And another thing that I find very important is this calculator, where you can evaluate expressions at the current state of your program, and just figure out what you can do.

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.

05:41 You can check those out. But just to give you an idea of the powers that you have when you’re using a good IDE like PyCharm. All right, that’s it for debugging.

05:49 Let’s look at a couple more awesome features in the next video. See you there.

Become a Member to join the conversation.