Modern Python Projects Transcripts
Chapter: Your code editor
Lecture: Debugging Python code

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