Setting Up VSCode For Python Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's going on guys so in this video I'm going to show you how to get visual studio codes set up for Python development and we're going to install a couple different extensions I have them disabled now but I'll enable each one and show you the features the most important is the actual Python extension because that gives us the ability for our intellisense and snippets and debugging testing all that stuff for so right now everything is disabled and I do have a Python file here and I can write Python code I can import a module like Jason but notice I have no autocomplete no intellisense I have no snippets or anything like that and I can't even properly debug so if I try to debug this we have to create a launch jason file and I can't even select Python here I don't have the ability to I can select node I have my node extension but no Python so the first thing we want to do is install the python extension so if we search for that you can see I have it installed but it's disabled and this does everything from allows allowing us to select environments creating virtual environments and using them intellisense debugging testing all that stuff so we wanted we want to enable this or install it and once I do that you can see down at the bottom corner here it says python 3 point 8.1 that's the version i have installed and if i click that you can choose different environments as well now in most cases you're going to want to create a virtual environment so that when you install packages and stuff it's isolated into that environment and you can use something like pipi and v we're actually going to use virtual env so I'm gonna take my Python 3 so I just want to show you if I do a - - version that's three point eight point one so if I say Python 3 - M V env so virtually in V and then a folder that I want to put that virtual environment in which I'll also call V and V if I run that it's going to create the folder and down here you'll see it's asking me if I want to use this new virtual environment and this is actually the Python extension that's that's making this pop up so we'll say yes and now if we look in the corner here you'll see I'm using that environment now we still have to activate it so in the VNV folder we have a bin folder with an activate script so we'll just say source go into V env slash bin slash activate and now it's activated and you can see this virtual env right here okay so now we should be all set as far as our environment so I'll show you some of the capabilities we have within our code here so we now have the ability to add snippets so if I just put an if for instance if it has this box right here this blank box that's a code snippet so I could go to like if-else hit enter and it gives me a little template for an if statement and I can tab through it so for instance I'll say true I'll hit tab it brings me in here I'll say print say print one tab again brings me into the else print two I'm not gonna keep this code but just to show you and then IFIF I hit escape it'll unhighlight everything if we want a try block so we have all these options here so we do that if we want a for loop we could do four and make sure you pick the one with a little box that's the snippet and that will give us a for loop that we can tab through okay and there's a bunch of snippets if you want to look at the docs for the Python extension you can see all the different snippets so now I want to look at autocomplete and intellisense so if we go ahead and import you'll see we get this drop down I could choose import I could choose Jason and then if I hover over this we get some information about what this module is some examples now this this obviously isn't very easy to read so I'm going to be using another extension called kite which has some really really nice documentation that I'll show you in a little bit Bob but I just want to go ahead and write a little bit of code here so I'm gonna set this to Jason dot and as soon as I hit dot it'll show me any properties or methods or anything that is associated with Jason so I'm going to choose loads and if we hover over it you can see that this will DC ray an instance containing a JSON document to a Python object so basically we can pass in here Jason and turn it into a Python dictionary or Python object so I'm just going to paste in a very simple JSON array with an ID and text fields and that's gonna put it into this variable and then what I'll do is go ahead and use my for snippet and let's say for item hit tab that will say items tab again and let's print and we'll print item and I want the text field now I'm just gonna mess this up on purpose real quick and put in title and save now to run the file of course we could just say Python main dot pi and that's just gonna throw an error but we can also use the debugger now so if I click on the debug our I'm sorry this one right here and click on this create a launch JSON file now we have the option for Python file we also have Django flask etc so I'm gonna choose Python file and it's just going to create this launch jason and we don't have to do anything to this we'll just keep the default here and now we should be able to use the debugger so where it says python current file I'm gonna click the play button and it's going to go through and run it and you can see that this is going to pop up here we have an exception a key error exception because there is no title and if we look under variables you'll see item and items which is the array and it shows us that there's an ID and a text ok there is no title so we could go ahead and we could fix that and then we could right we could reload this and it runs just fine if we look in the console it went ahead and loop through and it printed out item 1 and item 2 we can also put breakpoints so if I put a breakpoint here and I run the file it's gonna stop here notice that it doesn't show items over here because we haven't hit that point yet and we can either step over or step into with these functions here I'm just gonna hit this arrow hit it again and now you can see items over here if I hit this again now we see item hit it again and now it made it for its first loop and we can see item 1 hit it again it's gonna print item 2 now and it would just keep going through and if I hit again it should end okay so we can set our break points we can debug and that's possible because of the python extension now another extension that I like is a ripple so if we search for a repl so what this does says right here it automatically evaluates python code in real time as you type so if we have print statements it'll show it automatically we don't have to run our code it'll show any variables that we have stuff like that so I'm going to go ahead and enable this and once we enable it if we look up here to the right we have this little orange guy this icon if I click that it's going to open up a new window with kind of a landing page and as soon as I go over here and I type something that landing page will clear up and it'll show me any variables so you can see I have we have the item we have these items here this variable which is an array and then the print output is coming from this loop right here so instead of having to run the file and see the item 1 item 2 we can just see it here in fact if I were to add an extra item in the array here let's say 3 ID 3 you can see that now it's printing that out and that also got added here now where this really comes in handy is when you're dealing with api's and and the requests module so I'm actually going to create a new file here called main just call it main - dot pi and I want to use the request module so I have to install that I'm just going to close that up for now so we'll use pip to install requests okay and then I'm gonna go up here and import requests and let's open up a repple so it's not showing anything now because we don't have any variables or output but let's set a variable called response and any errors while we're typing out if we have errors it'll show that show them up here as well I'm going to set this to requests and there's a method called get which will make a get request if I hover over it you can read more about it and then I'm gonna make a request to the random user API just to get some random user data so HTTP and it's gonna be random user dot me slash API and if we look over here this response variable includes this request dot models dot response object and if we look in the state here it includes things like the status code which is 200 it was a successful request the URL the reason which is okay the encoding the cookies so stuff like that is all included in this response variable now the actual data to get that let's say data equals response dot jason and then you'll see that this data variable now includes info which is an object with the page the results and results which is an array of the items in this case it's just one item it's one object one user if we want to get more than one with this API we can do slash question mark results equals say 10 so now if we look in results we have 10 items so 10 users and we can see the fields we can see the values and we can now use this within our code so it comes in handy when you're dealing with stuff like this so let's say I want to loop through an output all the first names we can see that there is a name object with a first last in title property so let's do our for snippet we'll say for you in results I'm sorry data but data has a results array so we want to use that here so we want to loop through the results and let's go ahead and print and we know that there's a name object and then there's a first property and now automatically over here it's outputting all the first names okay if I save this I can run the file if I want to print them to the console I can clear this up I can either run Python dot main I can run the debugger or I can just click this little play icon and that will run the file and now you can see all the first names being printed out okay so this this can come in handy this a repple I really like it so the next extension that I'm going to show you is kite now kite is something that you want to install on your system because there's a system tool called co-pilot so I'll have a link in the description where you can download it install it and you can also install a plugin a plugin for atom or sublime text and some other editors as well but let's go ahead and enable it so here I'll say Nabal okay and now let's go back to main dot PI and let's say we want to know more about Jason now we do have this embedded intellisense but notice now we have this Doc's link and if I click this it's going to go ahead and open up co-pilot which is basically just an app running on our system and it shows us description and then all the examples and it's very neat so we can actually read it and see the examples and if we look at all the members like loads which is what we used over here it'll show us any possible arguments it'll show us the description stuff like that we can also search for things so like let's say we want to look at the cysts or this drop down here and this will show us the description it'll show us all the different members so classes functions so you can look up absolutely anything if we want to look at requests we could either search for it or we could go over here and just click Doc's and it'll open up requests so you can see the get get method that we used here sends a get request gives you some examples show you shows you how to use headers so very very helpful for just looking things up and for documentation so highly recommend the kite extension so the next one I want to show you is auto dock string which is really simple actually but it's it can be pretty helpful for documenting your actually this one I have enabled for documenting functions and stuff so if you want to install it and then we'll go to main dot I and let's just create just a very simple silly function so we'll call this call this greet and takes in a greeting and a name and then all I wanted to do is just return and we'll just return an F string with the greeting and the name so if I go right under where I define this I can create a doc string with triple triple double quotes and then just hit enter and it's going to show any arguments that were passed in and then the return down here and I can tab through this so for the summary I'll just say returns a greeting and hit tab and I can add a type to the first argument so string description it's a greet word and this is also a string description we'll say a person's name this return type is also a string and description a greeting with a name okay so I'll go ahead and save that I can open up a repple here and we can run it so if we say greet and pass in hello and pass in a string of world and you'll see that though we need to print it and now it prints out a Pierre hello world okay so pretty simple extension but again it can come in handy to document your functions so now I want to look at a little bit of testing I guess I'll save this I'm going to close those two files up and I'm not going to use another extension for this I'm going to use the Python extension which includes testing now we're going to use a module or a package called PI test and I'm gonna go to my settings real quick so if we go to settings and we search for tests and we click on python if we go down to right here PI tests enabled which is unchecked by default so I'm gonna go ahead and check it and it's going to ask us if we want to install PI tests so we can either we can click yes and it'll install or you could just do pip install PI test and then here is the directory where our tests are going to go in a folder called PI test and you can use other platforms as well I like PI tests it's easy to use honestly I don't write a ton of tests unless it's a large application but I did want to include this in the tutorial so you can see that we have this little lab icon over here so this is where we can run our tests now I'm going to create a file well let's create a folder first called PI tests and inside here I'm going to create a file called test underscore math dot pi so just do some simple math tests here and I'm gonna create a function oops wrong language let's create a function called add and this is just gonna take in two numbers so we'll say X and Y and we just want this to return X plus y and then I'm just going to write a test for this and most of the time what your testing will be in a different file you'll have to import it but just for simplicity I'm going to keep it here so I'm going to define test underscore add and from here we're going to assert ad and let's say one and two and let's make this fail by saying it equals four so I'm gonna go ahead and save this and now if we go back to the little test icon here and test Explorer so we don't have that that's a separate extension we could install but we want to just go down to where it says Python just reload and if we look at this we have test math dot pi and we just make this bigger and we can see each individual test that is in this file and we can run either the whole file with all the tests or the individual tests and we only have one right now so let's run the file and you can see that they failed we get this little red line here and tells us shows us the assertion error and then yeah so that failed let's change it to three and then let's run it again and now you can see we get green checks so it passed all right now let's create a couple more so I'll do keep this simple say subtract and we're just gonna X minus y and then we'll create another function oops didn't mean to bring that up so let's do increment which takes in a number and this will just return that number plus one all right and then we'll add our tests so it's a define test this is obviously very very simple usually your tests will be much more in-depth so subtract let's assert subtract function we'll say 1 & 2 I'm sorry 2 minus 1 and let's make it fail by saying 3 and then we'll do define Inc our define test Inc and will assert Inc three and will make this pass so that would be four because we're just adding one so if I save this and go over here and reload now you'll see that we have our test math with three different tests inside of it and we can run these separately so I can just run the ad or I can run them all and you can see the tests are tracked has failed okay it gives us this red line as well so let's make this pass by setting this to one because obviously two minus one is one so we'll go ahead and rerun that and now it pass okay so it's just some basic testing and there is an extension called test Explorer which does pretty much the same thing I think it has some extra features but we can actually I think I actually have it installed and that's why it's showing their Explorer or is it Python text test explore I mean yeah right here oh so it's let's go ahead and enable it so you can use this if you want so if we go back here and reload up here it says test Explorer we can see our tests here as well and we can run them from here you can see they all passed and this will actually put this stuff in the file like you can run it from here you can debug you can show in the test Explorer if they fail let's say this is three and we'll run that so that failed and then we can click show log and it'll show the assertion error down here all right so it has a couple more features but you don't have to use that that extra extension but but yeah that's just basic testing so I think that's gonna be it guys so we looked at you know setting up our environment setting up the Python extension with intellisense snippets we looked at a repple so that we can get the you know real-time variables and outputs and stuff like that testing so I mean this is the the setup that I use when I'm dealing with Python vs code if you have any other extensions or configurations or anything like that feel free to leave a comment and one other thing to deactivate your environment we can go back to our terminal here and clear this up and just say deactivate and now we're no longer using that we no longer have that environment activated so that's it guys thanks for watching and I'll see you next time
Info
Channel: Traversy Media
Views: 378,015
Rating: undefined out of 5
Keywords: python, python programming, vscode, python vscode, vscode python, python visual studio code, python setup
Id: W--_EOzdTHk
Channel Id: undefined
Length: 22min 19sec (1339 seconds)
Published: Fri Feb 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.