Visual Studio Code (Mac) - Setting up a Python Development Environment and Complete Overview

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there how's it going everybody in this video we're going to be learning how to install Visual Studio code and setup a Python development environment we're also going to go over the different features of this editor to see why so many people were switching over to this so we'll cover how to switch between python interpreters how to debug applications learn how git integration works look at unit testing capabilities and a few other things so I've had a ton of comments and requests to cover vs code and it just seems like so many people have switched over I don't know how many of you all listen to talk Python podcasts with Michael Kennedy but at the end of every show he always asks his guests which editor or IDE that they prefer to use and I feel like it used to be pretty mixed but now I hear vs code so often on there that I figured I had to take some time to try this out and I've got to say I'm really impressed with it so far I wouldn't doubt if you all see me using this from here on out in my videos so with that said let's go ahead and get started and see how to get this set up and I'll show you what I like so much about this now I did run into some differences between how this works on Mac and Windows so I decided to just make one video specifically for Mac and one specifically for Windows now in this video we're going to be covering how to use vs code on Mac but if you're using Windows then I'll be sure to leave a link to that video in the description section below so first of all let's install Visual Studio code so I've got their website pulled up here and my browser and that is that code Visual Studio com now Visual Studio code is different from Visual Studio which is a full IDE vs code is a lighter-weight editor that can be extended with plugins for whatever you need so be sure that you search for Visual Studio code and not just Visual Studio now I'll also have a link to this page in description section below to avoid any confusion there now vyas code is free so we don't need to buy anything we just need to download it and install it now for anyone who has watched my home brew tutorial then you can also download vs code through your terminal which with home brew cask if you find that easier but I'm going to download this from at their website so I'm just going to click download up here and now it's going to ask me to choose my operating system and I'm just going to choose the Mac and now it should download this here and this downloads into a zip file so I'm just going to unzip this then I can put this here in my applications so I'm going to happen open my applications in a new tab and I will just drop Visual Studio code into my applications there so now let's open that up so I will open up visual studio code here for the first time and it'll ask me if I'm sure I want to open it I'll say yes and now I will just go ahead and maximize this so at first when we first open this it opens up there getting started page here in your browser so I'm just going to ignore that but they do have a lot of nice information on there so let's open up the S code here now well enlarge this year so here we're greeted with their welcome screen and there's a lot of great information on here we can see that they have a printable keyboard cheat sheet here introductory videos tips and tricks how to can't change your color schemes and all kinds of stuff now we're going to cover a lot of this in the video so I'm not going to click on any of these right now so I'm just going to enlarge this a little bit so that we can see this a little bit easier and I'm also going to close this welcome page since we're going to go over this a lot of this stuff ourselves so we can see that this has a very simple design which is definitely nice once you get used to it because you don't have a lot of stuff in your way but when you're new it might be hard to remember your way around because we just mostly have icons instead of descriptions so this bar over here on the left is the activity bar and this is our main navigation for the editor so if you hover over any of these for any amount of time then it should pop up after a second or two with a short description so this says that it is the Explorer so if I click on this then this is where we can see our open directories and files now we don't have anything open right now but we have the option to open a project folder here now the second icon on the activity bar allows us to search through all of our files and so that's where we can do searches finds and replace within multiple files now the third icon here on the activity bar is our git integration and we'll take a look at this here in a bit and see how to push some code up the github but we are going to skip it for now now the next icon here the debugging icon now this is one very nice feature that vs code has over other simple editors I find the debugging pretty intuitive and easy within vs code and we'll take a look at that here in a bit as well when we debug some code okay so lastly the last icon on the activity bar is one that we're going to be using right now and this is for managing extensions and if we make this a little bit wider here then it should so show us some icons here okay so there those are okay so right off the bat it shows us some recommended extensions here at the top and then it shows us some popular extensions right below that now you might not have these recommended extensions if you do then they might not be the same ones so for example we can see that this is recommending Heroku CLI to me and that's because I have the Heroku CLI installed and if I actually click on one of these recommended extensions then at the top of the description here we can see that it says this extension is recommended because you have Heroku CLI installed so this will look in your system here and find a couple of other things that you already have installed and recommend plug-ins for you that they think you might like now it's kind of strange I would have thought that they would recommend that the sublime text plug-in here they usually do they did on my laptop but they're not here but usually it recommends this as well sublime text key mapping extension and what and it does say that this is recommended here so I'm not sure why it wasn't popping up in the recommended but what this does is they have a lot of different key mappings and Settings plug-ins for those where if you want to import your key mappings or settings from another program like sublime or maybe them or something like that then you could just install the plug-in and it'll do its best to match those settings to what you already have set there so I'm going to clear this out and now let's look at these popular extensions here now the extension that we're really after right now is the Python extension and this is the most popular extension on vs code by far so we can see here this is now sorted by popularity if I want to be sure that it's sorted by popularity then I could click up here and these three dots here and get this drop down and now we can see that we can sort these in some way so I can show the extensions by certain criteria so either recommended or popular so if I do show popular extensions then I can sort either by install count rating or name so right now it's sorted by rating and we can see that Python is here at the top with nearly 41 million installs and the next one below that doesn't even have half of that so this extension is definitely the most popular right now so we can see in the description here that it says that this extension adds support for linting debugging formatting unit testing and intellisense and intellisense is what shows us what attributes and methods we can use while we're typing and it also gives us information about those as well and we'll see that in action in just a bit so I'm going to install this python extension so I'll just click install here now this might take a second since this is a larger extension but it shouldn't take too long okay so that just now finished okay so now I'm going to open up some existing Python code that I have in a folder here on my desktop so let me open this up here and now I'm going to go to open folder and then I will go to my desktop here and open up this folder called my project and I have a script I file in here called script PI now if you don't have any Python code to work with right now then you can simply create a project for yourself and just add a dot py module in there to follow along now you do need to have a dot py extension so that it knows that it's a Python module and gives you some nice syntax highlighting so let me open up the script that I have here and I'm just going to close down a couple of these warnings here for now and we will look at those later let me make this just a little bit larger again so that we can be sure we can see everything here okay so here I've got a simple Python script so first I'm importing sis and I'm printing out sis top version and sis not executable now this will let me know what version of python i'm using and where it's located on my computer and I just have a simple function that creates a greeting and then I'm printing out a couple of greetings here at the bottom I just want some sample codes so that we can see what some of this syntax coloring looks like okay so first let's see how we can run at this Python code so by default our Python extension is going to use the first Python interpreter that it finds on our system path now if you don't have Python 3 installed then this might be using Python 2 and if it is and I'll show you how to change that in just a bit but for now let's just try to run this so to do this we can just right click whoops we can just right-click anywhere here in our module and towards the bottom we'll see a couple of run lines here so run current test file run python interpreter in terminal run selection line and python terminal we want this one here run python file in terminal so i'm going to click on that and that should run the python file that we have here in our terminal now we're gonna look at how to use keyboard shortcuts to run a code here in just a bit so if you don't like right clicking to run your code then don't worry we'll see how to make it more simple in just a second but for now if we look at my output then it looks like i'm using python 3.7 and it prints out the location to where that python is located on my machine and then it prints out our - greetings here hello world and hello quarry now what if we wanted to change our Python interpreter for how this runs our code now there are a couple of different ways that we can do this so first if we look at the blue bar here at the bottom of the page then in the bottom left it shows us which Python interpreter it is currently using so we can see here it says python 3.7 and if i click on that then we can change our interpreter so if we wanted to change to Python 2 we can see that this brings up a lot of options here because I have a few different versions of Python installed on my system here and also a couple of virtual environments here with Conda but if I wanted to do Python 2 then it's the one at the top here within user bin Python so I'm going to click on that and again I'm going to ignore the warnings there for now and we'll take a look at those in a bit and I will just clear my terminal down here and now I'm going to right-click and run Python file in terminal again now we can see this is using Python 2 7 and also the location of that Python executable changed as well now if this text is still a little bit too small here let me make it just a little bit larger I'm gonna change show you how to change the settings here in just a bit to where I can get this text looking exactly how I like it ok so that is using Python at - 7 but I want to go back to using Python 3 so again I'm just going to click down here in the bottom left and go and find a python @ 3 7 that I want to use so I'll click on that and now we are using python 3 7 again now if you don't have Python 3 installed then I definitely recommend installing it if you'd like to see how to install that on both Mac and Windows then I do have a separate video where I show how to do that for both operating systems so I'll be sure to put a link to that video in the description section below if anyone in needs to see how to install that and in that installation video I show how you can and get it in your path and everything like that so once it's in your path then vs code should automatically pick that up and allow you to use that by clicking down here in the bottom left and it should just automatically show up there now when I changed my interpreter you may or may not have noticed that vs code created a folder inside of my project directory here called dot vs code and that directory within it has a settings file settings dot JSON so these are settings specific to our current workspace or this current project folder so I'm going to open that up over here and we can see that right now it's setting that Python interpreter that we just set but what if we wanted to use a certain Python interpreter by default for every single project so to do that we're going to need to set it in our global user settings and while we're talking about our user settings let's also make some other global changes as well so let's look at how to change our color theme of our file icons and things like that so we can kind of personalize this editor a bit so first let's change our color now there are several built-in color themes that we can use so to change your color theme let's open up our command palette so this is the first time that we've opened the command palette but it is extremely useful you can basically access everything within vs code through the command palette so we can access that by pressing command shift P and it's going to open up the command palette here to where we can type in a command so now if we wanted to change some color themes then I'll just type in color and we can see that it starts Auto filling some results here so we can see that color theme is right down here so I'm going to open that up and now it gives us our options here and if I just hit up and down on the arrow keys here then this is automatically going to show us what this looks like as I'm going up and down here on the keyboard so that's a nice feature now if I have this maximized a lot so that you guys can read but usually this is a smaller window here to where we can actually see the syntax in the background and make our choice based on how that looks with our syntax highlighting now they do have some nice themes here by default but I actually want to go down here at the bottom to install additional color themes now if any of you have followed along with my videos before then you probably know that within sublime text my other editor that I use I really like a theme called pre-dawn and there actually is a port of this over on vs code as well so if we look at this the one that looks the same as the one in sublime is this pre-dawn theme kit this pre-dawn Twilight doesn't quite look the same so there's pre-dawn theme kit here if I install that it's also the one that has the most installs so I'm just going to choose the basic pre-dawn there and now if I go back to my script then we can see that these syntax highlighting changes to something that probably looks more familiar if you've seen my videos before and there's plenty of other popular color schemes out there so definitely look around and see what you like so as programmers you know we're in our editors a lot so you should definitely have something that is pleasant to your specific taste now we can also change how the file icons look over here in the sidebar and to do that we're going to install something called file icons so to install file icons we can open back up the command pilot by pressing command shift P and I will type in file icons choose a file icon theme now again we have a couple of defaults here but I am going to click on install additional file icon themes here and again if you ever want to sort these you can just click on this drop-down right here and we can sort by Install count rating and name so I'm going to sort by install count and it looks like it already was sorted by install count now the one that I like here is called a why you so I'm going to install that and that's going to be the one that I use so once we get that then I'm just going to choose that now that also changed our color theme so I'm just going to change our color theme back to pre Don really quick so I'll click on pre-dawn there so now I'm going to close down those two items there go back to my file explorer and now we can see that this still looks the same but now I'm going to open up that command palette again and we can see that it has my recently used commands here for color theme and file icon theme now I'm going to click on that file icon theme and change the file icon theme to that a Y you and I like that one we can see that it changed the icons over here and gives us a different icon here for open folders and stuff like that ok so I think that this looks pretty good so right now when we made those changes we're actually implicitly changing our user settings by changing our color themes and all of that but there are tons of other settings that we can change that we haven't seen yet so if we want to see all of the settings that we can change then we can open our settings by going down to the bottom left of our activity bar and clicking on this gear icon so I'm going to click on this gear icon and the other settings here there are command pilot or command palette settings extensions keyboard shortcuts color theme so we can also change those from here but I'm gonna click here on settings and open these and here we have our user settings now they used to have this set up to where it opened up the default settings and your user settings side by side in JSON format and I kind of liked that a bit better but now they have it opened up in this UI version instead so you can change your user settings using this user interface but honestly I hardly ever use this maybe it's because I'm coming from sublime text but I'd like to use the JSON settings a lot more than this and I feel like it just gives you a better overview of what you're changing but we can still get the JSON version of our user settings by clicking on this little bracket symbol here in the top right so I'm going to click on that and now it's going to open our user settings in JSON format so since these are our user settings this is only going to show us the settings that we have changed from the default so I've changed the zoom level here by zooming in and making the text larger now we have also changed the color theme here to pre don and the icon theme to a Y you and I have some other preferred settings that I'm gonna show in just a bit now I do wish that they still showed the default settings side by side by default because I feel like someone who's new to v/s code might not know what settings they can change or how to see the default settings so I'm gonna change my settings so that it does show my user settings and default settings side by side so to do this I'm first going to open up the default settings and I can do that using the command palette so I'm gonna press command shift P and now I'm going to type in default settings and we can see it says open default settings in JSON so I'm gonna click on that so now I'm going to change a few settings here that will show me the default settings while I'm modifying my user settings now you don't have to do what I'm doing here but I personally find it so much easier to modify my settings this way so I'm gonna search for work bench settings so up here in the top I'm going to press command F to do a search here and I'm going to search for work bench - settings so now we can see our work bench settings here so we have our editor and things like that so that is going to be the first one that I change so we can see that currently the workbench setting is set up to use the UI editor by default I'm going to change that to use JSON by default so I'm going to copy that and paste that into my user settings and I'm going to change that to JSON now a quick tip here and when you're within a settings if you click this little pencil icon over here to edit then it'll show you a drop-down of all the valid choices and I think that's extremely useful so we can choose UI there and this is telling me that I would have to save this first in order to do that so I could either choose UI or I could choose JSON okay so a couple of other settings that I wanted to change here down here this workbench settings not open default settings I want my default settings to open so that I can see all of those so I'm going to change that from false to true so I will save and try that again so now that is true and lastly I'm going to come down here and this workbench settings not use split JSON this is what's going to split my default settings from my user settings and this used to be the default with vs code but they changed it along the way so now that I've got that set to true I'm going to close down both of my settings tabs here and now I'm going to reopen those so I'm gonna click down here on the gear icon click on settings and now let me close my sidebar here so that we can see this a little bit better now we can see that it opens up my settings in JSON mode and also it shows me the default settings over here on the left with a comment of what each of these do and my user settings over here on the right so with these comments I now know exactly what each setting change actually means instead of just guessing over here and these are also split up into different categories so if I collapse commonly-used then we can see that if I want to change editor settings then I can look in here if I want to change Python settings I can scroll all the way down here to Python and there's a bunch of Python specific settings here so I think this is just much easier to see what is available to you now we started looking at these settings because I wanted to change my Python path by default to where it uses a certain version of Python for every file by default so I want to change that Python path so to do that I can come up here into search settings and now if I search for Python dot P then we can see we get a few matches here but one of these is Python dot path on a path and right now it's set to use just this basic Python command now instead of copying and pasting this over into my user settings when we have it set up this way we can just click on this little pencil icon over here to the left and if I do that then I can go to copy to settings and it automatically puts that over here in my user settings so now I want to put the full path to the version of Python that I want to use by default so I have my integrated terminal open down here at the bottom I want to get my path for python at 3 so to do that I'm just going to type in which python 3 and we can see that that prints out my path to Python 3 yours might be different than this so I'm going to copy that and I'm gonna paste that in here to my Python path so that it uses that for every file that we open so now anytime we're working with a Python file it should use that version of Python by default without us needing to change any Python interpreters now while we're in our settings you can also make other changes to your editor that you'd like to make so I'm going to copy and paste a few settings here into mine now you don't have to do this again they're just my personal preferences but let me grab these and I'll explain what some of these do so I'm going to copy these here and I will paste these in to my settings so within my user settings here I'm just going to paste these in below and save that so now ok the text is a little bit large here let me close down some of this stuff so that we can see a little bit better now the text is definitely too large here because I have this set on a zoom level of four so let me zoom out just a little bit and now so the zoom level will allow us to see things like the sidebar a little bit better but now our text is still a little bit too large so let me change this to a size of 20 actually let me do an 18 and I know that looks larger than an 18 font size but you have to remember we're also zoomed in to the editor so that's why it looks that large okay so I think an 18 font size with a zoom level of 3 allows us to see this text pretty well and also allows us to see the sidebar over here pretty well so I'm going to leave it like that now these other changes here I'm just also changing the font weight I'm changing the font family to be source code pro I like the source code pro from google fonts you can download that I'll leave a link to that in the description section below as well and we can also change the font size and the font family of our debug console and also the font size of our integrated terminal so that is what those setting changes are doing it there that I just pasted in so now I'm going to close down these settings and go back here to my script dot pie so now the s code should be using that version of Python that we set in our settings by default unless we change our interpreter specifically for our project now why would you want to change your specifically your interpreter specifically for one project well the most common reason is because it's usually a good idea to create a virtual environment for your projects so let's see how to create a virtual environment and then we'll set that as our interpreter for this specific project so I'll create one for the current project that we currently have open so to do this I'm going to open up my integrated terminal I think that's the easiest way to create these virtual environments so I'm going to open that up you can either drag here from the bottom or you can press control tilde and that'll open up the integrated terminal for you so I'm going to clear the terminal there and when you open up your integrated terminal it should already be navigated to our current project directory and that's where I'm going to create a virtual environment if you want to check your exact directory then you could always type in PWD and it will show you the directory that you're in so we can see that I am in that project folder on my desktop so I'm going to clear that output there so like I said I'm already within my project folder so now I'm going to create a new virtual environment using the VNV module from the standard library now if you don't know how to create virtual environments using V&V then I do have a separate video that goes into more detail about how to do that so I'll be sure to leave a link to that video in the description section below as well if anyone is interested but basically to do this we can simply say Python 3 or Python if you have it set up that way sometimes we have to use Python 3 so python 3 - m ve + V which is the module we're going to run and now the name of our virtual environment I'm also just going to call my virtual environment the env so I'm going to run that and that creates a virtual environment within our project folder now to use that for our project we just need to change the interpreter like we did earlier so I can click down here in the bottom left and when I do that if I look down here at the very bottom we can see that we have a Python 3 7 version and then this ve and V is within quotations here and we can see the path to this is in the current directory /v E and V so that is the virtual environment that we just created so I'm going to choose that as our Python interpreter for this specific project and again if we open up those vs code settings then that was set right there our Python path for this specific project uses this virtual environment forward slash bin bin forward slash Python so now I can close those so vs code automatically picked up that virtual environment because it picks up virtual environments within at the base of our project like we created here but it'll also pick up things like Khandava environments as well like we saw before now if you're using a virtual environment that's located elsewhere on your machine then to do that you can just you know open up the settings dot JSON file that we saw earlier and drop in the path into that settings dot JSON file to whatever virtual environment you want to use now you might keep getting these linter Python not installed down here and I just keep exiting these it's because it wants us to install a linter but the reason I keep exiting this is because we're gonna look at this in just a bit but I'm going to keep exiting that for now now when we have a virtual environment activated it will actually activate that virtual environment in all new terminal sessions that we open as well which is a really nice feature so let me delete the current terminal that I have open right now by selecting that trashcan and now I'm going to reopen a new one so I can just press control tilde like we did before and when we did that we can see that it automatically sourced and activated that environment and that V E and V environment is activated we can see it here within these brackets so now if we wanted to install some packages in this virtual environment then we should just be able to do this with a pip command so we don't even need to activate that I can just say pip install requests and once that's installed we should be able to just import that into our scripts with no problem so underneath this here I'm going to import requests so I'll import that and let me clear my terminal here so that we don't have so much text in the way I'll make this a little smaller as well so I'm gonna make sure that request is working by coming down here towards the bottom of this script and I'm gonna remove these current print statements with those greetings and instead I'm gonna replace those with a request so I can say R is equal to requests now as I'm typing here let's also pay attention to the intellisense that's built into vs code and see how this shows us the attributes and methods that we can use for different objects so I can choose requests here and now whenever I put in request dot it should recommend different things to us here so if I scroll down we can see that these methods are these little boxes here so we have git head options post things like that and we have all of this different documentation here as well so let's say I hovered over if I do request that git we can see that we also have the little information box over here so if I click on that then it'll show us over here what this expects so it expects a URL and also some of these parameters and a description of what this method does so that's a nice neat little trick there also if I click on that then I can right click on that method and we can see that we have a couple of options here we have go to definition we have peek definition so let me go to go to definition so what this did is it actually opened up where that method is defined within the request library so we're in our Python site packages here actually within that request code and we can actually see that get method or written out here now if we didn't want to totally open up that file then I can also right click that and go to peak definition and that's just gonna let us peek at that definition of where that function is defined so if I click that X over there then we go right back to our file so I think that's extremely useful the intellisense here is really smart and it really knows how to show us exactly what we're looking for so now let me actually request a URL here so I'm gonna go to let's see HTTP I'll just go to my personal website at Quarry MS com so over up here at the top I'm going to I'm just going to comment out the cyst on version that we're using I'm gonna you keep the sis not executable there so that I'm sure we're using our virtual environment but to test this I'm going to just do a print and I'm gonna print out our dot status code so the status code will just be the status code that we get back from that request so I'm going to save that and I'm going to run it so I'll right click run Python file in terminal and whenever we run that we can see that it did use that virtual environment and we got a status code of 200 ok so now that we've seen how to use these virtual environments now let's see how we can auto format our Python code so right now we don't have a formatter installed so we have seen those pop ups those pop-up notifications saying that we don't have formatters installed so I'm actually going to listen to pop up this time and actually use a formatter so to try to try to format our code manually we can use a keyboard shortcut so to do this it is shift option F so or shift alt F so I'm gonna use shift option F to try to format the code and now it's saying hey you don't have a formatter installed do you want to use auto pepp 8so auto Pepe is very popular and it's what they use by default I've actually heard some good things about black here and it also recommends black so I'm actually gonna click on that but it's just personal preference if you want to use auto Pepe then you can do that I'm going to install black and use that I actually don't even really know the differences between Auto Pepe and black but I did see a blog post on Kenneth writes his blog and he said that he uses black so I kind of want to give it a shot and see if I notice any differences okay so with that formatter installed we can go back up here to our code and now if I press shift option F then it will auto format that code for us now it might not have looked like it changed all that much because our code was already formatted kind of properly but let me just mess this up a bit so let's say that I just have really badly written code here and I didn't put any spaces between anything so I'm going to format this I'm gonna press shift option F and when I auto format that we can see that it now puts all those spaces in place where they should be and makes our clone our code a lot cleaner now sometimes when we change settings in our project sometimes it might actually only make those changes in our workplace settings and not in our actual global user settings so I'm going to open up the settings here real quick real quick and check this so within my workspace settings we can see that it changed our Python formatting provider to use black I don't think it put that in my user settings so let me check if it did it doesn't look like it did so I'm going to add that in there as well and if you ever want to see this a little bit better you can always close your sidebar there to see a little bit better while I'm in here my settings I'm also going to change my settings so that my code automatically formats any time I save a file so I could search for the settings that I need to change but let me show you a nice tip that I use a lot so if we start typing and press control space then it'll actually show us our options while we type so right underneath where I set my formatting provider there I'm also going to let's see if I press control space then we have all of our settings here so now I'm just going to type in format and we can see here that this fills in editor not format unsaved so that's another way that you can discover settings by doing that so right now it's set the false I want to set that equal to true so that our code is automatically formatted any time we save a file okay so now I'm just going to close down those settings now the auto formatting in vs code really is great it has auto auto formatting for JSON and other languages as well and those are all things that require additional packages with editors like sublime text and those can also be you know sometimes hard to get set up properly in those other editors so it's nice that this stuff just works so easily within vs code and I don't think that this is part of the formatter but we can also use vs code to sort our imports too so first let me add a couple of other imports up here so we can see what this looks like so I'm going to also import OS and I'm also going to import math now if I open up my command palette here then I'm going to search for sort imports and we can see it says Python refactor so work imports I'm going to click on that then we can see that it sorted those imports now I think that it separated out requests here since that's a third party package so it probably has all the third party packages and the standard library modules split up like that and if you have any from imports then it would put those closer to the bottom just because it looks better so for example if I was to come up here and say you know from OS import well import rename then I did a sort of imports again so sort imports then we can see that it moves that from import there to the bottom okay so now that we've done code formatting now let's also enable linting so we've been getting these linting warnings here this whole time so now we're actually going to use this so linting will look at our code and tell us if it thinks something is off and it's nice because you can you know this can keep you from making mistakes that you might not notice on your own so we could install this from the command palette I could install it here but if that pop-up doesn't pop up for you then you can also use the command palette so if I open up the command palette and type in linting then I'm just going to click here on run linting and when I do that that will trigger that pop-up for you so this might tell you that you don't have a linter of pilant installed so it uses pilot by default and I actually like pilot a lot so I'm going to keep that one and so if I want to install that we can just click install and it's going to run through that here within our terminal and now I'm going to put something in here that is going to trigger a linting error and a warning so let me comment out here or let me actually uncomment it where I'm printing out this system and instead let me accidentally use a print statement like I'm using Python 2 so if I save that then you can see that we get an underline here and if I hover over that then we can see that it says missing parentheses oops missing parentheses and call to print did you mean and then it has this long thing here now if we want to see that within our terminal we can click on this little problems section here and that way we can see that message there in the problem section as well so it says did you mean a print sis top version so then I can just go up here and I can fix that syntax mistake there now this is also going to give you hints about things that aren't specific errors either so for and boy if I was to go down here into my Greek function and I was to put a variable inside my function that I actually never use so let's say I create a variable called test set that equal to test and save that then after a second we can see that this gets a little green underlined here and if I hover over that then it'll say unused variable test and then it also says that down here in our problems gives us a little warning unused variable test so that's a nice way of getting those types of things pointed out usually things like you know unused variables are mistakes something that we actually didn't intend so I'm going to remove that and now says no problems have been detected in your workspace so far okay so now let me show you another extension that I like that I used to make running Python code a bit easier so so far we've been right-clicking and selecting run code in terminal and I'm not the biggest fan of that so I'd rather just have an easy keyboard shortcut that does this for us and also it doesn't show as much other stuff in the terminal so the extension that I'm going to install here is called code runner so I'm going to search for that in the extensions so I'm going to click on extensions here and I will clear our current output make this a little bit larger here so I'm going to search for an extension called code runner that's here at the top and I'm going to install that and once that's installed we'll see a little run icon here at the top right of the editor so there are a few other changes that I like to make to the settings of this extension but let's see what it does for us right out of the box so I'm going to close that extension there and also I'm going to make those a little bit smaller go back to my file explorer here so right out of the box if I click this Ronnie code up here at the top then we can see this actually didn't work it says import error no module named request and the reason it didn't work is because it's using the default Python command on my machine we can see that here it's just using Python so since just using that Python command it means that it's using Python 2 as well because usually on Mac the default Python command defaults to Python 2 so that's what we why we need to change the settings for this extension so let's set it to use the current Python interpreter that vs code is currently using so to do this I'm going to open up my settings so I'm going to close the sidebar so that we have some room here I'm gonna open up my settings here I'm gonna make these default settings a little smaller and there are a couple of settings that I'm going to change here at the bottom of my file now I looked up what settings I'm gonna change before I started this video and I have these written down here in front of me now you could search through and find these settings yourself but I'm just going to go ahead and write these out so first we have one that is code - runner and we want to change code runner executors map so I will type that out executors map and this is going to be equal to a JSON itself so within this JSON I can set what I want to run when we run Python scripts so for Python I want this to use so we're gonna add in a few things here so I'm going to use this dollar sign and say Python path and make sure that that's capitalized correctly with the uppercase P there Python path so that will use the Python path that V s code is using so if V s code is using your virtual environment it'll use your virtual environment if it's using the default Python path up here then it will use the default Python path here so now so we'll do Python path - U and then we want another dollar sign there full file name with the capitalization just like that okay now I should actually say where I found this this is actually in the code runner documentation if I open this cup and we scrolled down here to the bottom these are just things that you kind of have to look for and figure out on your own I figured this out before the video but if you want to make your own customizations then you'll have to read through documentation like this but we can see here in their documentation they say you know if you want to make certain changes these are the customized parameters so here we have that pythonpath with that dollar sign and it tells us what that does the path at the Python interpreter that is from the Select interpreter command and also our full file name the full name of the code of the file being run ok so that is how I find found those just as a side note okay so there are a couple of other changes I want to make here as well so I don't know about you all but whenever I run Python code I want the output just to be what the output is of the of the command that I'm running I don't really like having you know other stuff in the way like the terminal location and you know command took however long to run anything like that so first of all I'm going to change I'm going to do a code - runner of show execution message and I'm going to set that equal to false because that is what comes up with all this extra information of you know the Python path and the file path and stuff like that I just want the Python output okay so lastly I'm going to change code runner dot and I'm going to turn off clear previous output I'm going to set that equal to true actually because I don't want the old output showing up I only want the newest output okay so now let's go back to our script and see if this works now and we'll also be able to see the difference of what this does here so I'm going to comment out that sis top version again I'm going to leave that sis not executable so now that we have those changes in place I'm going to save our file and now if I click run here then we can see that this output is so much cleaner so it is now using our virtual environment so that's great so now it's using you know our virtual environments version of requests and then it just prints out our 200 status code there and there's no other you know stuff here getting in the way like file paths and stuff like that it is just our output now if you don't like clicking this run command up here then this does come with a keyboard shortcut as well so if I right click it says run code here we can see that that is control option and in so if I press control option in then we can see that our code runs like that as well and you can change that keyboard shortcut if you'd like okay so moving on one thing about sublime text that people always asked me about was how to do input and input was actually really hard to do within sublime there was a confusing package that you could add on it to do it but I never use that because it was just easier to use the terminal and that's what I would tell other people just open up your terminal and run your code that way if you have input but if we use vs code and we have input let me just add some in here so I'm just going to remove everything below my imports and now I'm just going to say name is equal to input and for the input I'll just say your name and then I will print out a greeting that says hello comma and then print out I don't need a space there and then print out name so we're not actually going to be able to run this using the code runner that we just installed because that output is only read-only and that was our problem with in sublime text as well but to run this in vs code we can simply do what we did earlier by right-clicking and running this within our terminal so if I do that then we can see that we have this running here within our terminal and now it's asking us for our input so I'll put in my name here and then it runs our script just fine so that worked it printed out hello Cori so that's just a little side note there that I wanted to show if you want to do input then input will work but you will have to use the terminal now I'm going to undo a couple of changes to go back to what we had before before that input okay all right so now let's take a look at the built-in git integration within vs code so let's say that I wanted to track this project with git and then upload this to github so to track our current project with git we can click on the source control tab over here on our activity bar so I'm going to click on that and when I do that so right now we're not tracking this project with git so I could do all of this here within the terminal but we can also do it here through the interface as well so to track this project I can just click on this get symbol here at the top of our tab right here beside the text of source control so if I click on that it says initialize repository so I'm going to do that and now it's asking us which project we want to track and I'm going to click the one that I have open there and now it is going to track this with git so the files that just popped up here are files that it wants to stage to be committed now it looks like we have a lot of extra files in our project right now but these were actually already there these are from our virtual environment and vs code is telling us that they're on stage now normally you don't want to track virtual environments so to ignore that we just need to create a dot git ignore file and add that to our repository so I'm going to go back to our project here and we can see that the get also adds in color coding here to tell us what's on stage and what's not so right now it's wanting to track our virtual environment we don't want that so right here by our project let's click on this file to create a new file or click on this icon to create a new file and I'm gonna create a new file called dot get ignore and we can see that it automatically fills in that get symbol there beside our file and now within not get ignore we want to ignore the V and V module and we also want to ignore I'm also going to put the vs code directory in there as well because I don't think we want to upload our settings and things like that those are more personal so now that we've saved those we can see that it's no longer color coding our vs code or our V E and V directory because it's not going to try to stage those anymore now you'll notice that it doesn't actually show us the dot get directory that got created when we initialize our directory vs code filters out what files are seen by default and ones that you normally don't want to see are filtered out so there's normally no reason to go into certain folders or files such as dot git so I think it's nice that it filters those out but if you ever want to make changes to what's shown or hidden then you can always update your settings and change the hidden files so now I'm going to go back to my git section here in my activity folder so now we can see that the only change is that it wants to stage our the got get ignore file and our script op py so to stage those changes I could hover over these one at a time and click the plus icon here beside each file so if I click that beside git ignore then we can see that now that went up into our stage changes or if we want to stage all of the changes then we can click on the extra options here at the top right with these three little dots and then I could just come down here to stage all changes and that'll add all of those to our stage section here so now that we have those files staged in order to commit those we can simply click here on this check mark so I'm gonna do that and it's going to ask us for a commit message so I'm just going to type in a commit message of initial commit and hit enter and now it has committed that code so we have our code committed to get so now it's only going to show us changes that we've made since then so if I go to my scripts and take out a lot of this stuff that we've put in here so let's say that I only wanted my module to contain the request code from now on so I'm going to take out the greet function I'm going to remove our print statements here I'm not using you know cysts or math or anything like that so I'm gonna remove those as well if I save that at auto formats that code for us so that's nice now this also gives us some indicators over here of what has changed for our git commit so if we click that then we can see that we can take a little peek at what's different so these red outlines here means that code has been removed so that's a nice touch there now if we go back to the git section and click on this then we can see the diff side-by-side and you can also change exactly how the diff is seen you can also layer them on top of each other but I really like the side-by-side look so we have our current file that is committed over here and then how our file looks right now and it's telling us hey we had all of this code here before and now it's gone so those are actually deletions that I wanted to make so that's fine so now I'm going to click on the plus icon to stage that file and now I'm going to commit that and when I commit that I'm just gonna leave a commit message that says removed some code okay and now that is committed okay so now let's say that we wanted to commit this code in this project up to github now this is pretty simple to do also all we have to do is create a repository within the browser and then we can set that up to push to that repo so I have my github open here within my browser so let me reload the page and make sure that I'm still logged in here and it looks like I am so that's good okay so now all I have to do is create a new repository I think I created one the other day called let's see demo repository sample repository I'm just gonna create a new one here and call it test - repo so I'm not going to do a description or anything like that so I can just create that repository and now whenever you create a new repository github will show you this quick setup page here so the commands that it shows us here are commands to create a new repository on the command line or we want if we want to push an existing repository from the command line now we already have an existing repository that we just created within ves code so this is the section that I am interested in here so I'm going to copy this get remote add origin here and that's what we're going to use to connect our local repository to this remote repository that we just created so I'm going to copy that and I'm going to go back to visual studio code and now I want to use my terminal to paste that in so if I first I want to shut down that this diff here so to open up my turmeric terminal that is control tilde and I'm going to clear the output there now I'm going to paste in the command that I got from github and I am here within my project so if I run that then that should now connect our git project in our local folder here to our remote repository so now if I go back to our git section here then I'm going to click here at the top right of our source control page and now I'm going to go down here to the bottom and actually it's up here too close to the top it is push - so I'm going to push - and now I'm going to push to origin and that the reason that we have origin there is because we pasted it in that git remote add origin command that we got from github so I'm going to push to that and that might take a second and it asks us here if we want to periodically run git fetch you can say yes or no I'm just going to say ask me later for now so now if I go back to the browser I'm going to look at my repository and we can see that we have a repository with two commits here and this is our code that we have within vs code so now that you have this connected to github any you can always come in here to visual studio code and make your updates and staging commit those like we saw before and then simply come up here and push those and it'll push those to github so that's how you can work with git and github and vs code and I think it's a pretty intuitive how they set that up and it's really nice to use now if you'd like to learn more about git then I do have a more detailed video on that and I'll leave a link to that video in the description section below if anyone is interested I also plan on doing a detailed video on github in the future but I haven't got around to doing that just yet but I just wanted to cover the basics in this video to get you started ok so now let's look at how to do some basic debugging in Python now this is a nice feature in vs code that doesn't come built in with some other minimalist editors like this so back in our script here let's put in I'm going to close that getting Noir file let's put in a breakpoint after we make our web request sorry so in order to put a breakpoint in we can just come over here into our gutter and whenever this red dot shows up we can just click there and that will add a breakpoint now I'll click on the debug tab of the activity bar over here and that's where we can start our debugger so first we're going to need to select a debug configuration so there are some more advanced to bug configurations that you can use for debugging applications like flask and Django but in this video let's just look at the debugging basics so we can click on add configuration and I just want to do a Python file and that'll create some configurations for us here let me make this a little bit larger here so now within our debug configuration we can see that we have this selection here for Python current file so in order to debug this I can simply click on this green button and it's going to debug this here within my terminal so what that does is it runs our code and it stops at our breakpoint that we added here on line 5 so now we can interact with our code as it is at the moment of that breakpoint so if you look here in the top left then it'll show you the current local values in our code and you can drill down into those to see the current values that we have set so we have this response up here we can look at all the values of that we can look at our request library and all kinds of different things there now underneath that variable section we have a watch section and within here we can add a variable there that we'd like to watch and monitor the current value so if I add a watch statement then let's say that I wanted to watch our dot status underscore code so if I watch that then we can see that the current value of that is 200 now if we were watching a value in a loop or something like that then we'd actually be able to see that increment each time through the loop if we had a breakpoint within the loop so that's a nice feature there now if you want something more interactive then you can open up the debug console down here within the terminal so let me open that and now we can use this debug console here to inspect anything that we'd like so let's say that I wanted to look at R for example so we can see that that is a response object of 200 and we can dig down into that here let's say I wanted to get more specific and look at the value of okay so if i run r dot okay we can see that that's equal to true if i look at our dot URL then it shows us the URL that we originally requested so being able to jump into the code at a specific location and see the current values can be extremely useful in a lot of situations so it's so much better than dropping down print statements or log statements all throughout your code and running your code over and over having this debug capability is so much better than that so this allows you to keep your code clean and avoid those print statements and log statements and also see all of the information that you need in order to make sure that your code is working how you expect and if it's not working then hopefully it'll help show you exactly how you can fix it so once we're at a breakpoint then we have several other options here so we can continue on or we can step over the current breakpoint step into the code further step out we can restart the debugger or we can stop so if I click on continue then there are no more breakpoints so that is just going to run through and complete our script okay so now I'm going to remove that debug breakpoint so I'm just going to click there and when we click there it removes that breakpoint okay so the last thing that we're going to take a look at in this video for Python specifically is the unit testing support that they have built in to vs code now I'm really loving their attention to detail and how they covered so many different aspects of helping us manage our code easily as easily as possible so having built in support for all this stuff is definitely a nice addition that you won't find in most other editors so to show you some unit testing I'm going to close down my current project and I'm going to open up a different project that has some sample unit tests so I close down vs code there and now I'm just going to open up my terminal here and now I'm going to navigate to my desktop because that is where I have my sample unit testing code now we could open this project like we did last time from within vs code but if you want to open a directory or file from the command-line then you can do that with the code command that should have been installed when we installed vs code so if I want to open that unit testing directory on my desktop then I could simply say code and then the name of that directory and that directory name is unit test demo so if I run that okay so this is saying a code command not found so sometimes it will automatically install that command for you and sometimes it won't so if I open up visual studio code let me show you how to install this if you run into that problem so if I open up the command palette command shift P if I type in shell then we can see here that we have one command that says install code command and path I'm going to click on that it says that that was successful so I'm going to open a new terminal here now I'll see you D to my desktop and i'll say code unit-tests demo and now it opens that up within Visual Studio code so that's a nice way within your command line to open up directories or files from your command line okay so I'm going to close that and also I'm going to close my other window of es code here and just use my unit testing code here okay so within my sample project here I have a couple of sample unit tests so I have test underscore calc and I have let me close these pop-ups there and I have test underscore employee so the first thing that we want to do here is we want to open up our command palette so I'll hit command shift P and I will type in discover and it's already auto completed this for me it is Python discover test so I'm going to click on that and this will come up with a pop-up here that will ask us to enable and configure the test framework so I'm going to click on that and now it's asking us what unit test framework we want to use now I'm using the unit test module from the standard library for these tests but if you're using PI test or nose then you can use those so select whichever one you are using here but I am selecting unit tests since that's what these tests are written in so I'm going to click on that and then click on the root directory because that's the directory containing the unit test and now we want to select the pattern to identify our test files so if we can see that we have a few different options here now my test files are written as test underscore and then the module name so that would be this option here so test underscore wildcard but however your tests are written you want to select your naming pattern so I'm going to select that one and now that is going to discover my test now I'm giving this pilot is not installed over here I'm just gonna click on do not show that again for now okay so now let me close down our test because we didn't cover those at first now let me reopen those now that those have been discovered and if I look here within test underscore calc we can see that since it's discovered our test it's added in some lines to our test file here where we can either run this entire test class here or we can even run a single test at a time so for example if I wanted to only run our test ad function here then I can just click on run test for that specific test so if I click on that then we can see that the check mark lets us know that the tests passed and I can run all of the tests if I run the test for this entire class so if I click on that then we can see that now all of these have a check mark here and also if I look down here in the status bar at the bottom then it says that all four of our tests have passed so that's a nice indicator there so now let me change one of these tests so that it fails so that we can see what this looks like so I'm gonna change one of the assertions and my test ad function to where this is going to fail so I'll say that if we add ten and five then it should equal ten but so if I save that and now rerun these tests here then now we can see that we get an X here so these have check marks this one has an X and we can see down here at the bottom that we have three check marks and one warning so if I click on that then it gives us a few options here so we can either rerun our tests or I can only run the failed tests or I can view the unit test output so let me take a look at that unit test output and that is what will show me where we have our failing tests so we can see it ran all of these and that we had a fail on test ad it'll also show us exactly what failed so it says that this assertion failed here fifteen is not equal to ten so that's why that failed so now that we have that information there then we can go in and either fix our code or fix our test so I'm going to fix the test then I will just rerun all of these and now we can see that all of those are passing now lastly we get an additional tab over here oops let me close down that terminal there now we also get an additional tab over here in our activity bar and this is a testing tab so let me click on that to check this out so it might still be telling us that our test is failing here if you hadn't actually manually come over here and rerun that test after it was failing but since we did rerun that test then we can see that all of those are now passing so just to see what this looks like let me make this failing again here so that we can see what a failing test looks like over here okay so now we can see we have our failing test so and all of these were passing so now if I come over here and fix that rerun that then we can see that let me rerun that again then we can see that now we get all of those green checkmarks now if we want to run all of the tests that we discovered then we can simply come up here to the top of our file here and click this button with the green checkmark here at the top and run them all so if I click on that then it'll run all of the tests and we can see that all of those passed again so I really think that this is an awesome interface for seeing all of our tests in one place and if something is failing then we can easily come in here and just rerun a specific test by running it individually instead of rerunning our entire test suite so I think that's an awesome feature built into vs code okay now there's one last thing that I want to show you in this video and it's only going to take a second so I have some other preferred settings that I haven't shown in this video and most of them are settings for changing how the editor looks in their fullscreen Zen mode and will allow me to run code without too much other stuff getting in the way so I just want to show these additional settings so that you can see exactly how I'll have my code vs code setup if you see me using this in teacher videos so I have those settings pulled up here in my browser and these are also available on my github page so this is just the raw version of what's available on my github so I'm going to grab those now I'm going to go back to the s code and I'm going to open up my settings here and I'm just going to replace all of my settings with what I grabbed from my github there now there's only a few additional changes here so that made the font size a little smaller because these are my personal settings let me change that back to B let's see what was that 18 I think for now so I will change that back to 18 while I'm recording the video actually that's still a little bit too small and that is because my zoom level is different for my personal preferences as well so I'll change that back up to what was that 3 I think okay yes so that is good for a while we're recording this so like I was saying these settings are mostly the same from what we just saw in this video but there are a couple of changes in here to how the Zen mode looks so we can see here the Zen mode I've got the center layout turned off the fullscreen turned off hide line numbers turned off stuff like that and if you didn't know there's in mode is just there distraction-free mode that allows you to focus more on your code and hides lots of the menus and things that you might find it distracting so let me open up one of my files here and let me actually open up Zen mode so to do that I'm just going to open up my command palette here and type in Zen and I will toggle Zen mode so I have these change to where my Zen mode now looks like this so we can see that we have a lot less distractions and it's basically just the code so if I run this I'm gonna run this using this over here and now we can see that our output is nice and clean too so if I do use vs code in future tutorials then this is likely how you're going to see me using it now the editor that I normally use for videos is sublime text and I really like sublime text for its minimalist look so that we can just focus on the code and the output but with Zimmy or with a Zen mode setup like this we have a nice minimalist setup in V s code as well so you might see me using this if I completely make the switch over to V s code okay so that's all I wanted to cover for the Python features that I wanted to show you in vs code now there's a ton more to learn about visual studio code itself but I might save that for another video since this one is getting really long so if anyone is interested then the additional things that I'd like to cover in a future video would just be some of the editor features so learning how to use the multi cursor functionality some of my favorite keyboard shortcuts and things like that but if you'd like to see some of these sheep keyboard shortcuts for yourself then they make it easy to learn these we can open our command palette and if we just search for keyboard shortcuts so let me search that then we can see here that one of the options is to open this keyboard shortcuts reference here and this opens up a PDF online that we can zoom into here and we can see all the keyboard shortcuts available to us here but like I said I'd like to do a video in the near future showing you some of my other favorite vs code features like the multi cursor functionality and also how to use some of those keyboard shortcuts that are listed on that reference page okay so I think that is going to do it for this video I hope you found this video helpful especially if you're trying to switch over to vs code for your Python development so personally I think they really knocked it out of the park with this editor so as you saw they have so many features that are built-in and the features that aren't built-in are really easy to set up and get installed and also get configured but if anyone has any questions about will be covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that these hiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you you
Info
Channel: Corey Schafer
Views: 254,422
Rating: undefined out of 5
Keywords: visual studio code, vs code, visual studio, vscode, python, vscode python, python visual studio, vscode setup, visual studio code setup, python development environment, python in visual studio code, vscode mac, visual studio mac, python vs code, vs code mac, python mac vscode, python mac vs code, python tutorial, corey schafer, python programming
Id: 06I63_p-2A4
Channel Id: undefined
Length: 75min 43sec (4543 seconds)
Published: Wed May 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.