 On today's Visual Studio Toolbox, Tyreek is going to show us how to get started with Python development using Visual Studio Code. Hi, welcome to Visual Studio Toolbox. I'm your host Robert Green, and joining me today is Tyreek White. Hey, Tyreek. Hi, Robert. Thanks for having me. Thanks for being on the show. Yeah. I'm Tyreek 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 as a C-sharp developer, 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. Python, the sweet spot for Python is quickly or 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 albeit a very simple web application, I still create a web application. That's the basis for Python. It's the scrappy language where people are able to prototype quickly, but it's also used for production code as well, which is super exciting. Okay. 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 and VS Code, and getting started experience. Then later, my colleague, Jeffrey, will be talking about data science and VS Code for Python developers. So that'll be another episode that we'll record after this one. Definitely. Showing people the behind the scenes of how it works here in the studio. Right. It would be why I'm wearing the same shirt. That's what I always said. 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, Vinconda, some formatters like Black and AutoPep8, as well as some popular linters like Pylint. Some fun facts about Python extension and VS Code, it's the most popular extension in VS Code marketplace. Really? More so than C-Sharp. Yes. That's interesting. Very cool. Even better, is Visual Studio Code is now the top free tool for Python developers. Cool. 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 so. Okay. Cool. So it's an extension which implies that you have to go get it. Yes. Yeah, exactly. You install VS Code, you don't have this Python extension. Right. All right. So let's take a step back. Let's show you how to download and install VS Visual Studio Code. So the link to get Visual Studio Code, if you don't have it, is code.visualstudio.com. You come to this excellent download page, and 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, and 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 or? No, you want the one that has the green star here. Okay. 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. So as you can see, I've already installed this, so it says uninstall which I'm not going to do, but there'll be an install badge that you click on. You can also see that we have some quick tips here on how to get started, the things I'm going to cover today. So you need to install VS Code, which we've done. You need to install the extension, which I've done. Okay. 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 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 to get all of these features of the Python extension. Got it. Sounds good. All right. Awesome. So now we got Code, we got the extension, we got Python. We do. We do. I have Python on my machine. Perfect. So let's get started. Let's get people accustomed to the UI here because it is a bit different to get onboarded. Okay. So now that I have the extension, as you can see, I have the file explorer here, and 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, and I have a lot of other things. VS Code comes in box with source control and so I can use my favorite repos, connect to those, and do all of that stuff I know and love. We also have the toolbar down here, which allows you to see, I'm signed into Azure currently. I have the live 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. Yeah. How does that sound? Sounds great. Another cool thing is by default, when you install code on VS Code on Windows, it adds code to VS Code to the path, and 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, and I'm going to create a new folder for us. I'm going to call it VS Toolbox, and I'm going to navigate to that folder, and 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 dot 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 you're command line focused or command line driven. So now what I'm going to do actually is create a new file. So 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, or sorry, excuse me, a Python file. Okay. .py is the file type, and as you can see, I am immediately to verify that that extension is activated. I actually have Python down here in my toolbar, and if I click on this, it gives me the list of all the different interpreters I have installed on my machine for Python. So you can see. Those all came when you installed Python? Well, I've installed multiple different versions. Okay. All right. For different reasons for testing for playing around. But these are all the interpreter versions I have. Us, the extension can pick up most of those if they're in the standard usage which they typically are when you install Python. So pretty cool stuff there. Cool. So we created our file, and so now what I want to do is I want to talk about the command palette, and so that's going to be super helpful for us because for developers who are a 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 allow for Python, and all those commands for Python are denoted by Python colon, and then 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 again I can start editing, we can 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, and 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, Robert? 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 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 the Windows command and then I'll also, we can just 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 pi short for Python dash three 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. And this module is called VM for short for virtual environment, which makes sense, VMs. And I'm actually going to call our virtual environment. Give me any name you want. What's your favorite folder name? My favorite folder name, 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 dot VS code 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. And 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. Okay. 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 actually- And that 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 created. 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. And 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 sentiment API, and it's under my recent, so why not just click it there? Okay. Cool. And 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 way to just create simple web applications. And to answer your question, Robert, I have a virtual environment here called VS Toolbox. And again, I pre-created this so it's 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 you're command line driven again, the way in which you're going to activate a virtual environment on a Windows machine is you're going to start with a virtual environment name, so VS Toolbox, 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 on 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 sort of that path to the activate file, what you can do on a Windows machine is control shift back tick, and 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. And let's explore the editor for a minute. So 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 note 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 is pylint. And so it is going to ask you if you want to use pylint. But we also support other linting tools like flakeate, et cetera. And 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. And so what that essentially means is that it can't find this package inside of my virtual environment. 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 text blob. And so what we can do actually is install those. And 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 with source control, you may send up your virtual environment so people can download 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 they need. 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 contained locally to this application. So I'm going to go pip install dash r and then requirements.txt and hit Enter. And 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 cached versions of these guys. And what's important to note is that while it installs, 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. And so what this is, as I said before, is a simple Flask App. So what I'm doing here is I'm essentially creating a Flask App that's contained inside this folder called startup. I have a simple home page here, which is just saying, Python loves VS Toolbox. And this is actually a really cool application because it's not only just a simple web application, but it's a sentiment API as 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 endpoint 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. And so very cool. It finished installing the packages for us and noticed 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. Cool stuff. OK. So now what I want us to do, we've explored this application. So let's actually run this application. There are a 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 in the spaces. And that's, again, the beauty of Python to your earlier point. And less than 20 lines of code, essentially, I can create a powerful sentiment API. And I have some to-dos for myself, actually. I need to actually build the API. So if I scroll down to line 13, this is actually the route that's creating that endpoint that will allow us to send a word in and get the sentiment back out. And 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 it. We're going to say it's positive. If it's less than zero, it's negative. And so what I'm going to do is I'm actually just going to write that out. So if, text blob, 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, dot polarity, is less than zero, then I'm going to reassign that sentiment. I'm going to say, OK, you're 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? Text blob dot sentiment. Oh, it's the beauty of the text blob package. Really? Sentiment built in? Yeah, 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? I've never seen a text box that I've ever used have that built into it. Right. So what it actually does is it passes in that message to the text blob module. And then it does some stuff under the hood, and you get the sentiment, polarity, et cetera. So we reassign that sentiment as negative. And then what I'm actually going to do is I'm going to return that sentiment 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 want to start doing that. And notice that when I click Start Debugging, it's going to present the debug configuration menu. And we have a list of popular templates that we know Python developers use. Remember earlier, I mentioned that this is a Flask app. And so you can see that Flask is in that configuration list. And so that's what I want, and I'm going to select it. The next step for debugging a web app is entering the path to my startup file. Remember I said that this startup 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 startup file that is creating my Flask app, which is startup forward slash app.py. And 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. And another cool thing is that the bug icon here becomes highlighted in white. So you can see that I'm also debugging as well. I guess it did. It did, yeah. 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 the individual studio, the menu choices at the top were all caps, and it was a big controversy? That actually predates my time on Microsoft. So anyway, I discovered that because it was a controversy. No, yeah. It was 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 set on line 15, I actually need to navigate to the URL that it's giving me here. 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 could either copy and paste it into my browser of choice, or I can just control click, which I'm going to do. You can see it says, Python loves VS Toolbox. And notice that because I set this break point inside of the app.route 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. Give me, what's the phrase that you love, or maybe want to know the sentence enough? I don't know. Let's say 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 crown. 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. And what it's done is it's hit this break point inside of our API. And let's explore the debug menu for a minute, excuse me. 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 can actually use the debug toolbar there. Or I can just F10. So I'm just going to use the GUI there. And you notice that as I step, it said, okay, remember we say that we determine 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. And 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. And 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. Yeah, 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 rerun it a different way. Still using the debugger, but a slightly different way. So this time to run this application with the breakpoint set on line 15, I'm going to use my debug icon that I mentioned earlier. And 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 dropdown 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 to that same debug configuration menu. And you may be wondering, oh Tyreek, 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 launch.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 of, it's a JSON format, an array with a list of different configurations. But notice that- Everything's saved. It's saved. 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 launch configuration saved. And this is 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 add 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'll 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. And so we're gonna hit our green play button. And 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. And in Boston, when we like something like this, we'd say it's wicked cool. Now, wicked has a sentiment of negative, but cool has a sentiment of positive. So let's see what happens. Let's see how Text Blob handles it, so wicked cool. Okay, and notice again, it's flashing orange. It means it hit the breakpoint and it's verified that it hits the breakpoint by the yellow line here. And what's also really cool is that all of the information you're getting in the variables window here, you can actually hover over variables here. Yep. And you get the tool tips, which it's not playing nice with me. There we go. It was shy at first, but here we go. You can see all the different properties. And again, to continue, I'm gonna press F5. And I'm gonna navigate back to this. Excellent. And it says wicked cool is positive. Yay. Very good. Yeah, it's very awesome. And again, to recap that, if you start debugging from the toolbar here, you don't get that configuration saved. And so, and actually to get that saved, to make you more productive the next time, you wanna use the debug icon and then 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 Windows user, the commands are there. You can then, the command I use that I didn't really touch on at the time, but now I'm going to is the pip install dash 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 and the way that I like to do it is actually using the bug icon. Right. Which takes me to the debug configuration menu. I can click that dropdown, hit add configuration. I can choose my configuration of what I wanna debug. About the second time you have to retype all of that because you didn't save it, you're gonna hit straight to here. Right, exactly, exactly. Yes. 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 wanna go back to the Python file that you're interested in and then hit the green run button. Okay. Pretty cool stuff. And that's about all I have for you guys today. Just talking about that getting started experience. Cool. So for more information, people can go to aka.ms forward slash VST forward slash Python VS code. Yeah. There's a getting started tutorial there. It's more information on Python within Visual Studio Code. Where it's a good place for people to go to learn Python in general. There's a lot of documentation online. Our getting started, I think tutorial, will teach you links out to other things, other material that is useful. But yeah, any, for example, if you wanna learn about Flask, the Flask documentation that they have is excellent. Okay. If you ever have any questions about getting started though and you wanna interact directly with the team, our Twitter handle, excuse me, 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. Yeah. There's a great introduction to not only Visual Studio Code for folks that haven't played around with it a lot but also how you get started using Python. That's awesome. Perfect, yeah. Thanks. Awesome, thank you. Hope you guys enjoyed that and I'll see you next time on Visual Studio Toolbox.