How To Debug Python Code In Visual Studio Code (VSCode)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this tutorial i'm going to show you how to debug python code in vs code don't forget to subscribe to my channel and i'm going to go ahead and get started with this tutorial here i have a simple python program that defines a class named language it has a constructor and it has two methods get name and message the message method simply prints out the name property of that object and here at the bottom i create an array of two objects python and javascript and store them in language variable then i for loop through that array and print out a message for each of the language objects by calling the message method start debugging you have to make sure you have the python extension so go to extensions type python select python and click on the install button now to start debugging your python program let's go ahead and close this first and all you have to do is press f5 so once you press f5 this thing pops up and you can select different options you can debug python module but we're going to choose debug python file which is the current file so here we just entered the debug mode the vs code debugger gives us these controls you can unpause your debugger once you stop at a breakpoint you can step over that stack you can step into that stack like a function and you can step out of a stack now these debugging features are going to become much more clear as you practice debugging your python code and the program executed without errors because it doesn't have any errors and as you can see we printed out the message for both objects debugging isn't always about fixing errors sometimes you just want to look up a value of some variable or print a message when some kind of condition is true in order to start doing any of that we need to create a break point a break point is a stopping point at which your program will hold so that you can take a look at what's going on in your program at that state to make a new break point at a specific line move your mouse over there and click add the line that you want to create a break point for as well add a red dot at that location so now hit f5 and we're going to start debugging this program so our execution flow just stopped at the line we marked with a breakpoint and we can analyze what's being passed into the name parameter of the constructor and that's python that's because when we're creating the language array we're passing python into that name property you can even assign a different value to the name property in the constructor as the program runs as you're debugging it so i'm going to change python to python3 now this program is going to continue running with this new value i just assign by changing it this can be really helpful for debugging different places of your program without actually changing your code now there's a bunch of different things you can also check in the debug pane but we're not going to go that deep in this tutorial and so i changed the variable but our debugger is paused so i'm going to go ahead and unpause the program to see what happens in order to do that i'm going to press f5 or click on this continue button over there and so as you can see in the console we have my name is python3 which was the variable we changed now let's go ahead and try something else right click on the breakpoint and select edit breakpoint and then choose expression which is the default option so i'm going to type name equals javascript and this basically means if this condition is met only then the breakpoint will be activated hit enter to confirm the expression and press f5 again to start debugging this python program now what's going to happen is that this breakpoint will activate only when our for loop goes through the javascript language object and this is why in the debugger's local variable view you will see name equals javascript so this is another way of looking at your program state when a particular expression is met and to exit the debugger press the stop button over there now let's right click on the break point again and this time we're going to make it do something different i'm going to select edit breakpoint again but this time i'll choose hit count now this will make the breakpoint execute only once based on the number you specify here and that's the number of times this place in your code has been executed so far so if you set this to zero basically this breakpoint will never execute if you set the hit count to 1 this breakpoint will execute only the first time and if you set it to 2 for example this breakpoint will execute only the second time your code is running at that point in your script and pretty much never again i pressed f5 and the breakpoint executed at the second iteration in the for loop now i'm going to remove the previous breakpoint and place another one at this line here with the print statement now i'm going to right click the breakpoint and go to edit breakpoint and select the third option which is log message this will make the debug console show this message every time this place in your code is executed you can also add variables in brackets like this so here i'm going to print out a message with the object itself and now i'm going to hit f5 to start debugging this python program and it's going to run as expected without errors and you will not see anything in the terminal but if you go to the debug console you will see those messages printing out the instance of the object that we specified in the breakpoints log message so this secondary debug output tab in a terminal outputs messages specifically from the breakpoint so guys pretty much this is all there is to debugging python scripts in visual studio code if you think i might have missed something important just post a comment on this video or subscribe to this channel if you want to see all kinds of coding tutorials because i'm always working on something next
Info
Channel: Ghost Together
Views: 41,485
Rating: undefined out of 5
Keywords: python, vscode, visual studio code, breakpoint, debugging, console, terminal, how to, output, debug console, vs code
Id: oCcTiRGPogQ
Channel Id: undefined
Length: 6min 38sec (398 seconds)
Published: Thu Sep 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.