Modern Python Projects Transcripts
Chapter: Your code editor
Lecture: Debugging Python code
0:00 In the previous lesson, I showed you how to run a single Python file. This time I open a folder with just a single file there.
0:08 But I want to show you what's the difference when you open a single file in VSCode versus when you open a folder that contains some files?
0:16 When we go to this debug menu, you see that when you open the folder, you now have an option took create a launch.json file.
0:23 That way, when we set up a launch configuration for our debugger, it will be persistent. So, let's click this and again we select flask and this
0:33 creates a launched.json file inside our folder. It's actually inside the .vscode and here we have it,
0:39 Here You can customize, how you want your flask application to be launched. The default values are good enough.
0:45 But if, for example, you want to enable the debugger, then you would remove this argument. Now that we have this launch.json file.
0:51 If we go back to the debug menu, you see that we have this launch configuration here, so we can simply press this green arrow to, launch our Debugger.
1:04 And again, this started the simple development server. We can see the website by refreshing this page.
1:11 As you can see, I've added a few more things to this file. So instead of displaying a static text,
1:17 I'm displaying my name and then a random integer between 0 and 100 which will be
1:22 our lucky number. I've added those two variables, so we can test how the debugging
1:26 works. You can add a break point by clicking this red dot on the left side. So, let's put a break point on this line.
1:33 Now, if we start the server again and we refresh the page, the code execution should stop at this line when it gets there.
1:41 So let's try it. We have the server, we go here, you can see it's still loading, and we go back to VSCode
1:51 and you have this indicator that we stopped here. On the left side, We can now see the locals and global variables.
1:59 Only name is defined at this moment, but if we go one step down, you can see that we have the lucky number defined and then the name, under the
2:09 variables menu. We have the watch menu, if you have a variable that you want to monitor, but it's not in the locals or in the global's.
2:17 You can add it here to this WATCH menu, so, let's add another variable to our code. Let's re-run it. Let's actually add an expression to WATCH.
2:37 Let's refresh the page and we go back to our break point, and now you can see, we can check. What's the value of this surname variable, under the WATCH
2:48 We have a CALL STACK. So, if you have a complicated code that calls multiple
2:52 functions, you would see the whole CALL STACK from the main function all the way down toward the break point is located.
2:58 We only have one function, so it's not really useful. But if you have a code with a lot of things going on, cause that can be really helpful,
3:07 and then finally you have list of break points because you can see we only have one in our app.py.
3:13 But you also can insert break points when an exception is raised or when an exception
3:18 is Uncaught, can also click here to deactivate all the break points or click here to add custom break point from this menu.
3:25 And if you want to execute some Python code in your debugging session, you can goto this debug console.
3:30 Here, you can run any Python expression, so you can inspect some of the existing variables. But you can also,
3:36 for example, modify them. So, if we check the value of, name its Sebastian. And if we try to change it, you can see that now, in the local scope,
3:46 name has changed to ‘Steven’. And if we continue the execution of this code and we go back to the browser you can see that our change has persisted.