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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Python Debugging With VS Code

Give Feedback

VS Code has a debugger that has many of the common features you expect in a debugger such as:

  • Variable tracking
  • Breakpoints
  • Call stack inspection
  • Watch expressions

In this video you’ll learn how to start the debugger, step over and into functions, set breakpoints, and setup a debug configuration for more complicated projects. You’ll also see how to control Python apps running in VS Code’s built in terminal or the external system terminal.

00:00 IntelliSense, combined with the debugging capabilities of the Python extension, make it easy for us to debug our Python programs. I’ve got a file called sum.py.

00:12 This is just a trivial little program that asks the user for two numbers and then adds them together and prints the result in a formatted string. Now, you might be able to see the bug here right away, but for demonstration purposes, let’s just pretend this is a much larger and more complex program and we have no idea where the bug is. I’ll right-click and run this program in the terminal, and now it’s going to prompt me for two numbers. I’ll use 4 for our a value and 5 for b. And when I press Enter, you’ll see that it’s concatenating those values instead of summing them.

00:51 So I’ll move my mouse along the gutter here and I’ll click to set a breakpoint on the line that calls our add() function. If you right-click on this expression, you’ll see that we can set different conditions that must be met in order for our breakpoint to actually hit.

01:07 But we’ll just hit Escape and leave this blank for now. Now, if I select Debug and start debugging, the program is going to run and I’ll be prompted to enter my numbers again.

01:20 The program takes in those inputs and now it’s stopped on our breakpoint. We’ll see that we have all of our local variables, all of our watch variables we can set, and we also have our call stack here at the bottom. At the top, we have our standard debugging controls, like Step Over, Step In, and Step Out.

01:42 I’ll click Step In here, because we want to inspect what’s happening within our function. And now you’ll see that execution has stopped on the return line.

01:52 The add() function has been pushed on to the call stack, and up here we can see that our local variables a and b are actually strings.

02:01 And of course, adding two strings is only going to concatenate them, which is what we don’t want. We can also see that by hovering over a and b within the editor, just like this.

02:13 You can hover over pretty much any part of your code to get more information about it. It’s one of those really helpful features in Visual Studio. Let’s stop this debugger and I’ll use the built-in int() function to convert our input values to integers before we pass them into the function.

02:32 And now our bug has been fixed. VS Code also supports debug configurations.

02:39 These are stored in a file called launch.json, which stores all of our launch configurations. To edit that file, choose Debug > Open Configurations.

02:51 Each one of these JSON objects represents a configuration for us to use.

02:57 Let’s copy this first configuration and paste it above. I’ll rename the configuration to "External Terminal", and now I’ll change the "console" value to "externalTerminal".

03:11 This will allow us to run and debug our Python programs with our system terminal instead of the one built into VS Code. Now I’ll save this with Command + S and I’ll switch back to my Python file.

03:26 I’ll delete the breakpoint, since we fixed our bug, and now notice that there’s a little dropdown menu next to this green arrow.

03:34 If we click on this, we can change the launch configuration to our new external terminal. Now we’ll select Debug > Start Debugging, and we’ll notice that the program will run in the external terminal instead of the internal one. In the next video, we’ll see how to work with source control management in VS Code.

Jon Nyquist on Aug. 13, 2019

Minor point, but you didn’t really fix the bug if the user was expecting to add floating point numbers.

rickecon on April 11, 2020

You should include the sum.py script in the notes below this video.

Become a Member to join the conversation.