Getting Started with Python in Visual Studio Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
>> On today's Visual Studio toolbox Tyrique is going to show us how to get started with Python development using Visual Studio Code. [MUSIC] >> Hi, welcome to Visual Studio Toolbox. I'm your host Robert Green and joining me today is Tyrique White. Hey Tyrique. >> Hi Robert, thanks for having me. >> Thanks for being on the show. >> Yeah, I'm Tyrique White everyone and I'm a program manager for Python developer tools here at Microsoft. >> Cool. So we're going to talk about doing some Python, we're going to show Visual Studio code. >> Right. >> That's awesome. So give us again the very brief because I think we've talked about it before, but the very brief sweet spot for Python. I is a C-sharp developer, or a JavaScript developer, when and why should I learn Python? What are the scenarios that are most prevalent? >> Definitely. So Robert, excellent question when we get a lot at conferences. The sweet spot for Python is being able to build apps quickly. One of my demos later I'm going to show in nine lines of code I'm able to create. I'll be at a very simple web application. I still create a web application. That's the basis for Python. It's the scrapping language where people are able to prototype quickly. But it's also used for production code as well, which is super exciting. >> Okay. >> Yeah, cool. >> Yeah, awesome. So today specifically we're going to talk about Python and VS Code. For any of those who don't know, VS Code is this free open-source cross-platform tool which uses a popularized extension model for every language. >> Right. >> So today we're going to talk about the Python extension in VS Code and getting started experience. Then later, my colleague Jeffrey will be talking about data science and VS Code for Python development. >> So that will be another episode that we'll record after this one. >> Definitely. >> Showing people behind the scenes and how it works here in the studio. >> Right. >> This way I'm wearing the same shirt. >> Awesome, cool. So let's get started. So the Python Extension offers a lot of great jam-packed features. It offers Intellisense, debugging, testing their support for virtual environment packages like VM and Conda, some formatters like Black and Autopep8, as well as some popular linters like pylints. Some fun facts about Python extension in VS Code, it's the most popular extension than the VS Code marketplace. >> Real, more certain than C-sharp. >> Yes. >> That's interesting. >> Even better is, Visual Studio Code is now at the top three tool for Python developers, which is really exciting. A lot of hype around Python and hopefully by the end of this you guys will understand a bit more why. That's all. >> So it's an extension which implies that you have to go get it? >> Yes, exactly. >> You install VS Code, you don't have this Python extension. >>Right. So yeah. Let's take a step back. Let's show you how to download and install Visual Studio Code. So the link to get Visual Studio Code if you don't have it is code.visualstudio.com and you come to this external and download page. As you can see it auto detects which OS I'm using. >> Okay. >> I can quickly grab a copy of VS Code. I already have it installed but I'm sure we get the idea there. Once you have Visual Studio Code you are going to open it, and you're going to see this welcome screen. This welcome screen has tools that allow you to customize. It has this wonderful activity bar here which gives you access to all of your extensions that you have installed. So after you download Visual Studio Code and you want to use Python, you have to go get the Python extension. >> Okay. >> So to do that you're going to go to the Extensions batch here, and I'm going to type Python. Because it's the most popular, it's going to come up right at the top and you're going to see the Python extension for Microsoft. >> Not the Python for VS Code or the Python extension? >> No. You want the one that has the Green star here. Good question. >> The one that you worked on. >> The one that I worked on. >> Not that there's anything wrong with those other ones, but this is the one you worked on? >> Definitely. As you can see, I've already installed this. So it says uninstall which I'm not going to do, but they'll be an install badge that can be click on. You can also see that we have some quick tips here on how to get started, kind of the things I'm going to cover today. So you need to install VS Code which you've done that. You need to install the extension which I've done. Then the third thing you need is an installation of Python on your machine. >> Right. >> Yeah. One misconception we find is that some people often think that the Python extension for VS Code it comes with Python and it does not currently. So one way to get around that is depending on which OS you're on. For Windows, we recommend going to python.org which I have here to download the latest release of Python, on Mac a brew install works fine and for Linux, typically the bundled version of Python that comes with the Linux works well, you get all of these features of the Python extension. >> Got it. >> Sound good. >> All right. Awesome. >>So now we've got code, we got the extension, we got Python. >> We do, I have python. Perfect. Okay. So let's get started. Let's get people accustomed to the UI here, because it is a bit different to get on-boarded. >> Okay. >> So now that I have the extension. As you can see, I have the File Explorer here. When I open a folder, I'm going to be able to see the tree view of that folder, see all the dependencies and all the files within there. I have a debugger icon which we'll talk about later. I have a lot of other things. VS Code comes inbox with source control. So I can use my favorite repos, connect to those, and do all of that stuff I know and love. We also have the tool bar down here, which allows you to see. I'm signed into Azure currently. I have the life share extension installed. I have my toolbar up here, all that good stuff. >> Okay. >> So what I now want to do is create a folder and let's get started playing around some code. How does that sound? >> Sounds great. >> Another cool thing is, by default when you install VS Code on Windows, it adds VS Code to the path. I'll show you exactly what that means. So what I want to do is I want to navigate to my Command Prompt, and I'm going to find my Demos folder that I have here. I'm going to create a new folder for us. I'm going to call it VS toolbox. I'm going to navigate to that folder. Now what's really cool is, because I have VS Code on the path on my Windows machine, all I have to do is type code. into the Command Prompt, and it will open up an instance of VS Code inside of that folder. >> Okay. >> You can see that from my File Explorer here. >> So you can open the folder from code, you can go to the folder and launch code from that location? >> Exactly. On Linux and Mac you have to run a command to get VS Code on path, but it's super handy if your command line focus. So now what I'm going to do actually is create a new file. This is the important part of activating the Python extension. In order for the extension to activate, which you're seeing down there, activating extension, you need to select a Python folder. Sorry, excuse me, a Python file. >> Okay. >>.Py is the file type. As you can see I'm immediately, to verify that extension is activated, I actually have Python down here in my toolbar. If I click on this, it gives me the list of all the different interpreters I have installed on my machine for Python. >> Is it okay when you installed Python? >> Well, I've installed multiple different versions for different reasons; for testing, for a playing around. But these are all the interpreter versions I have. As the extension can pick up most of those if they are in the standard places which they typically are when you install Python. So pretty cool stuff there. So we created our file. So now what I want to do is I want to talk about the command palette. That's going to be super helpful for us because for developers who are bit more Command Line driven, you want to have that terminal experience inside the editor. So in order to get to what I call the Command Palette, you're going to do on a Windows machine Control Shift P, to bring up the Command Palette. Again, the Command Palette has all of the commands you could ever want and love for Python. All those commands of Python or denoted by Python colon. >> Okay. >> You can scroll down the entire list to get anything that you want. I am after create terminal, because I want to pull up a terminal for some command line driven stuff that I'm going to talk about now. >> Okay. >> Cool. So what's really cool here is that I can start editing or start writing some code. So if I write print, you can see that immediately I'm starting to get what we call intellisense as well as some documentation. So let's say I wasn't familiar with the print statement, I can scroll over to this documentation tool tip here, and I can see exactly what parameters it takes. For this I just want to print "Hello world". >> Okay. >> Cool. To run this, this is actually something we've recently added a couple of releases ago, is the green Run button. So I can actually just run my code with this button and it prints out in the terminal, "Hello World". >> Cool. >> Our classic example for coding,"Hello world". >>All right. >>Perfect. That's cool. Now, the next thing I want to talk about which is really important for Python development is the concept of a virtual environment. Are you familiar with virtual environments router? >> Not that much, no. >> Okay. So I'll explain briefly what those are. So in Python you can have application A and B. Let's say I'm a developer, I'm working on two different applications. However, each application requires separate versions of packages in order to function correctly. >> Okay. >> If I were to include all of those different package versions in the same single application or same environment, there's a possibility that I might have conflicts. Some code may not run, something may get broken. So typically what we recommend for Python developers is to create a virtual environment which I'm going to show you how to do now. Cool. So back to my command line here. So I'm in my VS toolbox folder. I have my hello.Py file, print "Hello world", we're all good. So there are different commands depending on the OS you're using. I'm actually going to show you when we're on those command. Then also we can look at the Linux, Mac command. But in order to run, to create a virtual environment from the command line, what I want to do is I want to type Py, short for Python -3 for the version of Python I'm going to use. Dash m, which is saying, okay, I'm going to pass a module that I want you to use. This module is called VM, short for virtual environment which makes sense. I'm actually going to call our virtual environments. Give me any name you want. What's your favorite folder name? >> My favorite folder name, pictures. >> Pictures. Cool. >> What's really cool is you notice this pop-up that comes. It says. "We noticed a new virtual environment has been created, do you want to select it for the workspace folder?" What's really cool about that is if I select "Yes" and navigate to my.vscode folder here, you'll see that we include this line with a path to that pictures virtual environment. So the next time I open this folder, I'll already be inside of this virtual environment which you can see is being denoted here pictures virtual environment, and so I can get more productive more quickly the next time. >> That's not an actual folder on the hard drive, right? >> So it creates a local copy, actually good question. It creates a local copy of Python for this workspace, and you can see the list of scripts that this includes as well as all of the libraries that you may have in that virtual environment. But again, it's just local to this workspace, so that way your application can get all of the stuff inside of that virtual environment. So I actually. >> It sticks around, if I exit code come back in I can have access to that environment or do I have to recreate it? >> Exactly, you have access to the environment. So what I'm actually going to do now is "Hello world" is exciting, but I actually want to show you a web app that I have with the virtual environment already create. >> Okay. Cool. >> So let's actually answer your question right now. So in order to close this I'm going to go to File, Close folder. There are a couple of ways I can open a new folder. I can go to File, Open Folder here, and navigate on my File Explorer. There's the Start Menu which allows me to open folder, but actually the demo I want is "Sentimentapi" and it's under my recent. So why not just click it there? Cool. So a couple of things to immediately notice about this application, is it's a simple web application using the popular Python package flask which is a weighted just create simple web applications, and to answer your question Robert, I have a virtual environment here called "vstoolbox". Again, I pre-created this so it saved when I opened it back up. What's even better is that I could actually activate that virtual environment inside the terminal in two ways. So if your command line driven again the way in which you're going to activate a virtual environment on a Windows machine is you can start with the virtual environment name, so vstoolbox backslash because we're on Windows, scripts backslash activate and you'll see that the virtual environment is activated by this parentheses with the virtual environment name. >> So then does that information store like in source control. So if I take this app, put it in GitHub, go home, clone it, and bring it down locally I have not only the code but also the environment information? >> Yeah, absolutely. If you want to share the virtual environment with developers in your team or with friends you definitely can do that. I'll also show you or you can just create a new one, but I'll show you. We've talked a little bit about that but I think it'll come full circle in a minute. Another quick way you can activate this virtual environment, if you don't want to have to remember that path to the activate file, what you can do on a Windows machine is control shift back tick. What it'll do is it'll open a new terminal for you with this virtual environment already activated. So some really cool stuff here. Let's explore the editor for a minute. So you guys or everyone has probably noticed the yellow squiggles that are appearing here. These yellow squiggles are what we call linting messages, and so what's important to know is that the first time you install the Python extension it's going to ask if you want to install a linter. The default currently as Pylint, and so it is going to ask you if you want to use Pylint, but we also support other lenting tools like Flake8 etc. So what you're seeing here are these linting messages. So if I hover over those linting messages, it's telling me, it's saying, "Hey, I can't find unresolved import text blob." So what that essentially means, is that, it can't find this package inside of my virtual environments. So it's giving me an error which is super useful because it gives me a code diagnostic or a problem which I can also view under problems here, and you see that I have the list of problems, unresolved import Flask, unresolved import textblob. So what we can do actually is install those. One thing that's also different about this folder compared to the last folder we created, is that, I have this requirements.txt file here, and for people who don't know for Python developers this is just the list of all the dependencies that my application needs to function properly. So back to your original point. Depending on the kind of setup you prefer what source control, you may send up your virtual environments so people can download it and just activate it or you might just send up the requirements.txt file and allow people to first create a virtual environment and then install all the packages thing here. So what I'm going to do is I'm going to install those packages from this requirements.txt file, so back to my command line notice again that I'm still inside this virtual environment because I want all these packages contains locally to this application. So I'm going to go pip install -r and then requirements.txt and hit "Enter". What you're going to notice is you're going to notice that it's going to find those packages and install them for me. It's getting the list, I've downloaded these before so it's just using the cache diversions of these guys. What's important to note is that when it installs these you're going to notice that these error messages are going to go away, which is really cool. So while it's installing those packages let's explore this application a bit. So what this is, as I said before, is the simple Flask gap. So what I'm doing here is I'm essentially creating a Flask gap that's contained inside this folder called Startup. I have a simple homepage here which is just saying, "Python loves vstoolbox," and this is actually a really cool application because it's not only just a simple web application but it's the sentimentapi is the title of the folder denotes. So what this is going to allow us to do, is it's going to allow us to hit an end point with a Word and this API is going to help us determine the sentiment of that word because maybe I just need help determining what kind of sentiment words have. So very cool it finished installing the packages for us, and notice that my problems went away, and suddenly if I hover over Flask I get documentation for it. Text blob I Get-module text blob which is really cool stuff. So now what I want us to do we explore this application. So let's actually run this application. There are couple of ways you can run it. I want to run it using the debugger, because I also want to use this as a moment to talk about debugging and kind of what that gets you. As I stated previously, this is a sentiment API pretty simple only less than perhaps 10 lines of code if you take out all the comments and the spaces, and that's again the beauty of Python's your earlier point in less than 20 lines of code, essentially, I can create a powerful sentiment API. I have some to do for myself actually, I need to actually build the API. So if I scroll down to line 13, this is actually the routes that's creating that endpoint that will allow us to send a word in and get the sentiment back out. So what I want to do is how this module is working is it's saying it's going to take in that message if the sentiment score is greater than zero we're going to determine, we're going to say it's positive. It's less than zero its negative. So what I'm going to do is I'm actually going to write that out. So if textblob I'm using that package and notice I'm getting my IntelliSense here. So I'm going to tap to accept that message. Again, is what I want. So if the message has a sentiment and notice that again this IntelliSense with the documentation which is allowing me to learn as I type.polarity is less than zero, then I'm going to reassign that sentiment, I'm going to say, "Okay your negative." Trying to type at the same time and let's get rid of my comment here. >> So how does text blob have a sentiment property? >> Sorry? >> Textblob.sentiment. >> Oh, it's the beauty of the textblob package. >> Really, it comes with sentiment built-in? >> Yeah. >> That's cool. >> Which is another cool thing about Python, I could very well create my own sentiment API from scratch if I wanted to, but why reinvent the wheel? >> Because I've never seen a text box that I've ever used have that built into it. >> Right. So what it actually does that passes in that message to the textblock module and then it does some stuff under the hood then with cool sentiment polarity etc. >> Cool. >> So we reassign that sentiment has negative, and then what I'm actually going to do is I'm going to return that sentiment on the page, on the web application page which you'll see in a second. So cool. Let's run the application. >> All right. >> I'm going to run it using the debugger actually because there are a couple of things I want to talk about. So let's set a breakpoint on line 15, notice that I clicked to the left of the line number which is called the gutter. For those of you who are used to debugging it's going to be the same kind of feel, for those who aren't fear not it's super easy to get started with the debugger. So to start this, there are a couple of ways I can run my debugger and I'm going to just collapse that so I can give you full attention here. I can use the toolbar, click "Debug" and click "Start debugging." It also has the keyboard shortcut F5 and so what I'm going to do is I'm going to start doing that. Notice that when I click "Start debugging" it's going to present to debug configuration menu. We have a list of popular templates that we know Python developers use. Remember earlier I mentioned that this is a Flask gap? >> Yeah. >> So you can see that Flask is in that configuration list, and so that's what I wanted and I'm going to select it. The next step for debugging a web app is entering the path to my start-up file. Remember I said that this start-up file is contained inside of a folder called Startup, so what I actually want to do is I want to tweak this path a little bit to actually point to my start-up file that is creating my flask gap which is startup/app.py. Once I hit "Enter" it's going to start debugging for me and notice that this toolbar down here changes to orange to denote that I'm in debugging mode. Another cool thing is that debug icon here becomes highlighted in white, so you can see that I'm also debugging as well. >> I guess it did. >> It did, yeah. >> Okay. >> I've never noticed that. >> Yeah. It's a pretty cool nifty thing. >> But at the same time, do you remember, I never tend to notice these things back, you remember the big controversy over in Visual Studio the menu choices at the top are all caps and it was a big controversy. >> That actually predates my time in Microsoft. >> Anyway, I discovered that because there was a controversy. >> No, yeah. >> Enough about me, let's get back to the app. >> Yeah. So to debug a web app, another thing that people getting started with web apps may not know is that in order to hit that break point that I've sent on line 15, I actually need to navigate to the URL that it's giving me here. >> Okay. >> So it tells me that it's running on this URL, and what's really cool is that inside VS code in order to get to that URL, I can either copy and paste it into my browser of choice, or I can just control click, which I'm going to do. >> Oh, look at that. >> You can see it says "Python loves VS toolbox". Notice that because I've set this break point inside of the app.py that defines that message, I actually, in order to hit that break point, I need to send a message to this API. So let's do it. What's the phrase that you love or maybe want to know the sentiment of, I don't know. >> Let's say "Champion". >> "Champion", let's see what the sentiment of champion is. So notice that I have this wheel spinning here. >> Because we are in the middle of the World Series and either today or tomorrow there will be a champion grounds. >> Oh, wow. Well let's see what the sentiment is. So you can tell that VS Code, the icon here is blinking orange, which means that it did something magical. What it's done is it's hit this break point inside of our API. Let's explore the debug menu for a minutes. So notice that it got our message "Champion". This is our locals variable window. I can also have my watch window, I have my call stack, all of this good stuff, and I can also step through lines. So to step over, I could actually use the debug toolbar there or I can just "F10", so I'm just going to use the [inaudible] there. You notice that as I step, it's said "okay" remember we say that we determined all sentiments to be positive at first, and then we run them through this if statement. So I'm just going to step over again. In order to continue, I can either hit the arrow here to continue and run it all the way through, or I can just hit "F5". So I'm going to hit "Continue" again. If I navigate back to my API, I see that the word "Champion" is positive. >> Nice. Yeah, pretty cool. >> All right, let's do another test. >> Let's do another test. >> Yes. Let's test a new message. >> Okay, let's test a new message. This time, I'm going to rerun this, but I'm going to try and rerun it a different way, I'm still using the debugger but a slightly different way. So this time, to run this application with the break point set on line 15, I'm going to use my debug icon that I mentioned earlier. Here's what's really cool about this is that the debug icon gives me access to the configuration menu that I saw before. So if I click this "drop-down" and select "Add configuration", what it's going to do and then I have to hit the green "play" button, start debugging, it's going to send me into that same debug configuration menu. >> Okay. >> You may be wondering, "Oh Tyrique, why do I have to type this again?", and it's because our configuration last time wasn't saved, and so by going through these steps we're going through now, we're saving that configuration and I'll prove that to you in a second. So I'm going to navigate to Flask again, put that path, and notice that it creates this file called "launched.json". We find that this launch file is very scary or intimidating for people who are first starting out with VS code, but fear not, it's simply an array. It's a JSON format, an array with a list of different configurations. But notice that. >> Everything's saved. >> It saves, right, and my startup path is saved. I have some different arguments here which are on by default, and to show you where it gets saved, if you navigate back to your file explorer by using the explorer button here, the dot VS code folder, if there wasn't one created before, it'll be created for you and you'll have that logic configuration saved. This is an important because, again, you don't have to go through the startup steps again to save it. So let's go back to my debugger, and you notice, see it says "this time" instead of "AD configuration" or "no configuration" like it said before, I get Python flask. Another important thing to realize about debugging is that, notice that I'm currently inside of the "launch.json" file. If I start the debugger here it will actually throw an error because it thinks I'm trying to debug this JSON. So in order to avoid that, I need to go back actually to the file that I'm trying to debug. So we're going to hit our green "play" button. Again, I need to navigate to. So let's try another word. >> All right. So this is a test of the sentiment. So I'm from Boston. >> Okay. >> In Boston when we like something like this, we'd say "It's wicked cool". >> "Wicked cool". >> Now "Wicked" has the sentiment of negative but "Cool" has the sentiment of positive. So let's see what happens. >> Let's see how it's Text bot handles it. So "Wicked cool". Okay. Notice again, it's flashing orange, it means it hit the break point, and it's verified that it hits the break point by the yellow line here. What's also really cool is that all of the information you're getting in the variables one here, you can actually hover over variables here. >> Yeah. >> You get the tool tips which it's not playing nice, there we go, it was shy at first, but here we go. You can see all the different properties. Again, to continue, I'm going to press "F5". I'm going to navigate back to this. >> Excellent. >> It says "Wicked cool is positive". >> All right. Yes. >> It's pretty cool. >> Very good. >> Yes, it's very awesome. Again, to recap that, if you start debugging from the toolbar here, you don't get that configuration saved. So in actually to get that saved, to make you more productive than next time, you want to use the debug icon and then an add a configuration. >> Cool. >> Which is really really cool stuff. So let's recap a little bit of what we talked about because I know that we've covered a lot. So using virtual environments, which we talked about to help you manage packages for different applications. So creating new environments, depending on if you're a Mac or Linux user or a Windows user, the commands are there. >> Okay. >> You can then the command I use, that I didn't really touch on at the time but now I'm going to as the pip install - r. requirements.txt, that's if your file or if your project has one of those, you can create a virtual environment first and then you can install the requirements that are there. You can also install individual module names if you want to. >> Okay. >> Getting started with debugging. Of course there are multiple ways you can do this, but the way that I recommend, in the way that I like to do it, is actually using the bug icon which takes me to the debug configuration menu I can click that "Drop-down", hit "App configuration", I can choose my configuration of what I want to debug. >> The second time you have to retype all of the actions because you didn't save it, you're going to hit straight to here. >> Right, exactly. Put the path to that web application, and that "launch.json" gets created, and then remember, you don't want to debug the "launch.json" file, you actually want to go back to the Python file that you are interested in and then hit the green run button. >> Okay. >> Pretty cool stuff and that's about all I have for you guys today. >> All right. >> Just talking about that getting started experience. >> Cool. So for more information, people can go to aka.ms /VST /pythonvscode. >> Yeah. >> There's a Getting Started tutorial there. More information on Python within Visual Studio Code. Where is a good place for people to go to learn Python in general? >> There's a lot of documentation online, our getting started tutorial, we'll teach you links out to other things, other material that is useful, but yes, for example, if you want to learn about Flask, the Flask documentation that they have is excellent. >> Okay. >> If you ever have any questions about getting started though and you want to interact directly with the team, our Twitter handle is at Python VS code. >> Okay. >> All one word, and we are always interacting with users trying to understand what experiences work best and which don't. >> Fantastic. >> Yeah. Awesome. Thanks for having me. >> Thanks for coming on. Those are great introduction to not only Visual Studio code for folks that haven't been playing around with it a lot but also how you get started using Python and it's awesome. >> Perfect. Yes. >> Thanks. >> Awesome. Thank you. >> Hope you guys enjoyed that and we will see you next time on Visual Studio Toolbox. [MUSIC]
Info
Channel: Microsoft Visual Studio
Views: 170,043
Rating: 4.8525596 out of 5
Keywords:
Id: 7EXd4_ttIuw
Channel Id: undefined
Length: 32min 47sec (1967 seconds)
Published: Tue Dec 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.