Debugging Python with Visual Studio Code (VSCode)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now let me add a very simple python script here which have two numbers and I'm just comparing them using if statement so once you have your script you have multiple options to run your script so you can right click on the same editor in which you are writing your code and then you can see you have this option to run a python file in the terminal you also have the Run current file in the interactive window so you have all these options here also you have this run button which is which is going to run your python file or if you are a fan of command prompt or terminal then you can directly type your command for example Python and then the name of the file which is going to run your python script in this terminal so let's say I'm going to choose this option to run my python script I'm going to press this button you can see my python script is execute muted and the result is here right so it's going to just select the interpreter from here and then the complete path of the file and it's going to run this file and then give you the out let's say I want to just run this script using terminal I can just type python here and then the name of the file which is test Dot py and it's going to give me the output which is lot cleaner than using this terminal because it's going to show you all the complete parts of the Python executable and your file right so whatever option seems to be useful for you you can use that option now let's say you ran your python script and let's say you want to debug your python script so you can just go to your script and then at the break point on your python script using these red dots right so you can just hover over the line on which you want to set the breakpoint and then just click there and you will see this kind of Red Dot here that means the breakpoint is set on the third line now on the left hand side you have this debug and run Icon here so you can click on debug and run and here at the bottom you will be able to see all break points you have set so right now I only have one breakpoint I can set multiple breakpoints here and I will be able to see the list of all the breakpoints here from here I can disable all the breakpoints by just clicking on this icon which means all the breakpoints are there but they are disabled right if I want to remove all the breakpoints I can click on the remove all breakpoints icon here and it's going to remove all the breakpoints so let me add the breakpoint once again now for the first time when you are debugging your python script when you click on the debug icon gear you will see this run and debug button here and also you will see uh this option which says to customize run and debug you can create a launch dot Json file so here you will be able to customize your debug file for example which debugger you want to use for your debugging which python interpreter you want to use which file you want to use for debugging so all these options you can give in the launch.json file let me just click on run and debug option which is going to give me all these options I want to choose the first option which is python file and once I click on that I will be able to see that my debugger is running and my program execution will stop at the break point here so you can see my execution is stopped at the break point I can see all the debugging related options here so when I click on this continue button my program will run normally I can just step over I can step into a function I can come out of the function and I can rerun my debugger using this option and I can stop this debugger using this icon here right when you see this pink kind of bar here that means your debugger is running right if you stop it you will see that this pink bar is gone right so this is kind of a trick to observe when your debugger is running or not right so let me rerun it once again you can see the debugger is running program execution is stopped at this breakpoint I will be able to see the list of all the local variables on the left hand side I can watch some variables let's say I want to watch a variable B I can just type B and then press enter and it's going to give me the value of the variable which I want to watch here I can also see the globals here so you can see all the global variable values are here and I can just tap over right so I can just go to the next execution point and see what's happening in my script so this is how you can debug your script let me just stop the debugger I can once again go to my folder Explorer now once you have added the python extension you will be able to see that the python intellisense is also there so when you type some letters of a method let's say it's going to show you all the methods which you can use related to what you have typed in your editor so when I type PRI it's going to show me all the print related options right I can also when I just write one parenthesis it's going to complete that I can print the value of let's say B right so all the intellisense options are available for me once I have installed my python extension I can just save my script by just pressing Ctrl s which is going to save my script now let's say you are an advanced user and you want to use a python virtual and in your project right so normally let's say whenever you want to install a python pip package you can do it directly but if you want to create a virtual end for your project you can also do it easily in Visual Studio code so let me create a new test file let me name it as test2 Dot py and here in my test2.py I have this very simple python script which uses a package called bcrypt right right now you can see this yellow squiggly a line here which says import bcrypt could not be resolved because we haven't installed this package using pip on our project so let me just close all these terminals I just want to use one terminal which is the command prompt and let me clear this command prompt here right so in order to create a new virtual environment you can type this command so python hyphen M then v n and then n and then press enter which is going to create a new virtual end for us and you will be able to see this warning which says we noticed a new virtual environment has been created do you want to select it for the workspace folder so when I choose yes here see what happens when I choose yes you will be able to see this virtual end is listed here so with my python interpreter this end is also visible you can see and we end right so now I can choose between the virtual and also from this interpreter list okay so you can choose your virtual and also so to activate this virtual and I can give this command and then press enter which is going to activate my virtual amp and you will be able to recognize using this so if you see the name end here then you have activated the virtual end so if you see the name of your virtual environment that means it's activated and then we can just install this package here so I can just write pip space hyphen Q install bcrypt which is my package Edge which I want to install and once I have installed this package you can see the squiggly line is gone that means I can just use this B crypto package in my script easily now right so now I can just type python command and then the name of the file which is test2 dot Piva py right so that is test2 Dot py and then press enter which is going to run this script and it runs successfully also when you are done you can just type deactivate which is going to deactivate your virtual environment so I can type deactivate and then press enter and now I'm in my normal command prompt also once you have created the virtual environment you will be able to see this directory here and right so all the configuration you will be able to see here and of course you will have the track of your files in your Version Control inside your Visual Studio code editor using the source control option so if you have installed git it's going to show you what changes you have made and it's going to help you to commit your code push your code pull your code from GitHub or any other repository easily inside the visual studio code the last option which I want to show here is let's say you don't want to just run and debug you want to create a launch dot Json file for debugging you can click on the Creator launch.json file and then click on python file and it's going to create this launch.json file so as I said so as I said you have the information about the console here the program which you want to run and then the request is here the type is python the name will be python current file and if you have created multiple debug files you will be able to choose those files or configurations using this list right and when you go to the Explorer you will be able to see this launch.json file which is created under dot vs code folder so this is a hidden directory and you will be able to change the content inside it using this launch dot Json file
Info
Channel: ProgrammingKnowledge
Views: 93,203
Rating: undefined out of 5
Keywords: python debugger, vscode python debugger not working, python debugger online, vscode debug python with arguments, vscode launch.json python, python remote debugging vscode, how to debug python code in pycharm, python debugger pycharm
Id: b4p-SBjHh28
Channel Id: undefined
Length: 11min 35sec (695 seconds)
Published: Wed Apr 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.