Movatterモバイル変換


[0]ホーム

URL:


Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Python Debugging With VS Code

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:00IntelliSense, combined with the debugging capabilities of the Python extension,make it easy for us to debug our Python programs.I’ve got a file calledsum.py.

00:12This is just a trivial little program that asks the user for two numbers andthen 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 programand we have no idea where the bug is. I’ll right-clickand run this program in the terminal,and now it’s going to prompt me for two numbers. I’ll use4 for oura value and5 forb. And when I press Enter,you’ll see that it’s concatenating those values instead of summing them.

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

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

01:20The 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:42I’ll click Step In here, because we want to inspect what’s happening within ourfunction. And now you’ll see that execution has stopped on thereturn line.

01:52Theadd() function has been pushed on to the call stack, and up herewe can see that our local variablesa andb are actually strings.

02:01And 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 overa andb within the editor,just like this.

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

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

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

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

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

03:11This will allow us to run and debug our Python programs with our system terminalinstead 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:26I’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:34If we click on this,we can change the launch configuration to our new external terminal.Now we’ll selectDebug >Start Debugging,and we’ll notice that the program will run in the external terminal instead ofthe internal one. In the next video,we’ll see how to work with source control management in VS Code.

Avatar image for Jon Nyquist

Jon Nyquist onAug. 13, 2019

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

Avatar image for rickecon

rickecon onApril 11, 2020

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

Become a Member to join the conversation.

Course Contents

Overview
83%

[8]ページ先頭

©2009-2026 Movatter.jp