VS Code Setup with Python Virtual Environments

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back to my network automation video series in this video we're going to be talking about visual studio code and specifically how to use Python virtual environment within Visual Studio code kind of how do we get started in that but first I want to talk a little bit about why Visual Studio code so most of us since for network engineers didn't start out using specific programming environments and programming text editors we started out with things like notepad plus plus or txt or angular maybe if you're on a Mac but a more powerful text editor but pretty much text editors on the other side of that spectrum you're gonna have developers who started out an IT using very powerful development environments like NetBeans or Eclipse and those give you some great tools as far as like looking in the classes on your programming but it's probably not something we're gonna need for scripting for Network automation so kind of in the middle of that there's this sweet spot that's come out and things like Visual Studio code are in there as well as there's also atom and sublime text as well and these are going to be extensible programs you say that or it extensible again or you can install add-ons to have them do so many different things so with Visual Studio code we're going to install a Python add-on and what that's going to allow us to do is it's gonna do a lot of the harder work in Python like just formatting and making sure you don't have any syntax errors it takes care of a lot of that and it could also do debugging you can do debugging from right within the program it's a super handy feature and things outside of Python you can tie in a github so you can have all your github repositories and pushing and pulling all done right from the program there so it's a really powerful tool and I found myself using it just as a normal text editor as well for a day-to-day use it's just what I go to now so it's definitely the way to go as far as programming Python in the network engineer world so let's go ahead and take a look at that so here we are on a fairly fresh install Visual Studio code so when you first open it up you're gonna see this welcome page here where you can start a new file or open a folder but what I want to do first is I want to go to the command and the command palettes where you're going to do a lot of your configuration changes for visual studio code itself so if we go up to the menu here view command palette there's a keyboard shortcut as well to get there but from this command palette what I want to do is install code command in path and what this is gonna let us do you can see at the bottom right here says it's successfully installed the code command and path this will let us do is we can actually start Visual Studio code from the command prompt now and one reason that I like that is instead of going to open folder or navigating through a GUI here to open something what you can do is let's quit Visual Studio code here and drop back to a command prompt so here we are back at our command prompt and we are in the same folder we were working on in the last video where we have SNMP polar code and virtual environment so I want to start Visual Studio code from right here instead of having to navigate through it to it through a menu so what we can do now that we've installed code in our path it's just like code dot and that's actually going to launch Visual Studio code right from this folder so there it's going to launch and you can see on this File Explorer on the left hand side we have our code with requirements txt and then we also have our virtual environment that we created so the next thing I want to do is actually open up a new python file so if i right click on this and do a new file and let's just do test by and you can see it popped up a little notification here at the bottoms and that the python extension is recommended for this type of file and asking if we want to install it we do but i'm not going to install it right here let's actually go to the extension store and see how you can do that for any extension so if we go back to that command palette and type extensions you can see install extensions here so let's go ahead and run that and you can see here it's recommending python so if we click on python here it is you're gonna see a little description about it how many people have downloaded it what's star rating it so it's pretty much like an app store for visual studio code add-ons so this clearly is the right one it's from Microsoft 19 million downloads 4 and 3/4 stars so it looks like what we want to install so let's go ahead and install that and that's gonna give Visual Studio code the ability to work more with Python so that's what's going to allow it to do auto completion and it can do what's called Linton linting is going to point out errors in your code so if you have a syntax error it'll highlight that automatically for you so what we want to do is let's just go and do a print hello world and if you notice I didn't actually have to close that parenthesis or quote it did all that for me it'll do a lot more Auto formatting and now my favorite thing is indentation Python is very sensitive to whitespace and it takes care of most of that for you so because this is a Python program and it recognizes that we have an option here to just hit play we can actually run the Python file in a terminal so let's go ahead and do that but first let's go ahead and set our virtual environment up so if we just run it now it's gonna run in the global python environment and like we talked about in the last video that's not always the best for managing all your dependencies so if we go back to that command palette and we type python select interpreter that's the one we want right there so these are all the different Python interpreters that we can run and because we're running from the folder that we're working in where it had the SNMP polar virtual environment yeah had that folder listed so it's searching for that right now so we can see that it knows about that so we can run that just click that and now when we hit play you can see it popped up a terminal window down here and did that same command we did in the last video source in our SNMP polar virtual environment and activated it and then it ran that piece of code and showed HelloWorld there but so it knew about that because we were working from this folder and it had the virtual environment listed within it but what if you have all your virtual environments stored somewhere else what if you keep a separate folder you're just working in some one-off area and it doesn't necessarily have your virtual environment listed how do you do that well everything in Visual Studio code is actually stored in a settings JSON file and so we can see what it's doing here so let's go back to our command palette let's sleep workspace so if we open a workspace settings and then if we go here to the upper right there's this little icon and it says open settings JSON so if we do that here you can see what it did so it created this file called settings JSON and within it it's saying Python path SNMP polar virtual environment bin Python so that's where it's actually selecting our virtual environment so if you ever go up to your command palette you go to select interpreter and you're trying to find a virtual environment and for some reason it's not listed here or you're not in the right folder it's just not seeing it you can go to workspace settings and then open the settings JSON file and you can do it by hand with this Python Python path and it'll actually kind of autocomplete that for you as you start typing so it's pretty intelligent so that is how you are going to install Python within Visual Studio code also get Visual Studio code to your path so you can launch it from the command line and two different ways to do your virtual environment either through the command palette or through the settings JSON in our next video we're gonna start looking at github and how to integrate that individual studio code as well thanks for watching bye
Info
Channel: Nathan Loop - Network Automation
Views: 30,189
Rating: 3.8659003 out of 5
Keywords:
Id: TSH4C1Zipcs
Channel Id: undefined
Length: 8min 50sec (530 seconds)
Published: Thu Apr 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.