How To Setup Python for VSCode | Setting Up VSCode For Python Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can set up the support for python on Visual Studio code on your Windows operating system so I'm assuming that you have already installed Visual Studio code and python on your Windows operating system if you don't know how to install Visual Studio code or python I will provide the link of those videos in the description so you can watch those videos and then come to this video right so once you already have Python and visual studio code on your Windows operating system we are ready to go so first of all we can check the version of python for example so just write CMD on your terminal and if you have a python installed then this python version command is going to give you the version of python in my case it's 3.10.0 in your case it can be different depending on when you have installed python on your Windows operating system as you can see I already also have Visual Studio code now let me open the terminal once again and let me just start from the basics so I'm assuming you are totally new and you just want to create the first python project in your Visual Studio code and debug it and run it and then use Python scripts inside your Visual Studio code so let's say I want to create a folder which will be my project folder on the desktop right so I'm going to just CD to the desktop here in my command prompt and then press enter and here I can create a folder let's say I want to create a folder named work so I can just type mkdir command which is uh the command line option to create a folder in your command prompt on Windows operating system and then the name of your folder so let's say in my case I want to create a work directory on my desktop so I can just press enter and you can see this directory is created I will CD into this directory and then press enter once again so now I'm in my work directory and let's say here I want to create all the python script for my project so now here I can just type code space Dot and then press enter which is going to open my work directory in Visual Studio code you can see the name of the directory here this name will always appear in capital letters so if you are wondering you have created this directory in all small letters but this directory is created in capital letters here on Visual Studio code you will always see the directory name in in all capital letters so now let's create a python file you can create a python file by just right clicking on the Explorer here or you can click on this option which says new file or you can create the file by just clicking on the file and then click on new file so whatever option is easier for you just use that option and then just create your python file I'm going to name my file as test Dot py and then press enter now as soon as I create a python file Visual Studio code is going to recognize that I want to create python projects or Python scripts and it's going to give me the suggestion for installing the extension for python you can see do you want to install the recommended extension for python you can click on the install button which is going to install the support for python otherwise what you can do you can click on the extensions icon here and then it's going to show you the list of all the popular extensions and you can also search for the extensions so let's say I want the python extension so I will type python here and it's going to show me all the extensions related to python now you will also see these options are sorted by their popularity right so you can see the number of downloads for this first option is 48 million then 3.8 million then 2.6 million so I'm going to select the first option because it's mostly used here you can see who has developed this extension so it's developed by Microsoft that's good because Visual Studio code is developed by Microsoft so this will be the best extension for the support of python and then I can also see the ratings here and the number of downloads once again I can click on the install button which is going to install this extension which in turn is going to add the support for python in Visual Studio code so once this extension is installed you will be able to see these two options here you have the option to uninstall this extension and you have the option to disable this extension so if in future you want to develop let's say Java or some other language you can maybe disable python support or totally uninstall it using these two options you can also just scroll down and read how you can use this extension right so once this extension is installed then we are ready to write some code also you will observe so let me just move to the Explorer you will observe here your python interpreter right so as you might have seen I have python 3.10.0 installed so visual studio code is going to recognize my a python interpreter and it's going to show this interpreter here if I have multiple versions of python installed or if I have python installed in multiple places on my windows 11 operating system then Visual Studio code is going to recognize that and it's going to show all these options here so when I click on this interpreter option I have only one python installation which is recognized by Visual Studio code but if I have multiple installation and multiple versions of python I will be able to select a particular interpreter for my python script if I have Anaconda installed then also I will be able to see the Anaconda related options here so you can choose the interpreter from here also when you type Ctrl shift p you will be able to see this kind of search bar here when you type python you will be able to see all these options related to python so you can see create a terminal configure tests debug python file enable disable linking go to python object so you can see you have all the options here on this command palette here so you can search all the options related to python now if you want to open the integrated terminal to just run your script you have this option which says terminal and then when you click on new terminal it is going to open the terminal inside the visual studio code and you will be able to see which terminal is opened so you can see it's a Powershell terminal you can also see this here if you want to open the command prompt and not Powershell you can just choose it from here so you can click here and then click on command prompt and it's going to open the command prompt and you will be able to see the list of all the command prompt terminals which are opened on your Visual Studio code 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 you have all these options here also you have this run button 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 am going to choose this option to run my python script I'm going to press this button you can see my python script is executed 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 ah terminal because it is 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 add 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 breakpoints 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 n 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 NV 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 Test 2 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 so that's it for this video I hope you have understood the basics of getting started with python on Visual Studio code I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge2
Views: 27,299
Rating: undefined out of 5
Keywords: how to run python code in visual studio code, how to run python program in visual studio code terminal, visual studio code python, python extension for visual studio code, how to install pip in visual studio code, python download, visual studio code download, python interpreter vscode
Id: oSmEIr3yBL0
Channel Id: undefined
Length: 20min 12sec (1212 seconds)
Published: Fri Mar 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.