Getting Started with Python in Visual Studio Code
Tyrique White sets up Python in VS Code: the Python extension, interpreters and virtual environments, a small Flask sentiment API with TextBlob, and debugging.
字幕來源:創作者上傳至 YouTube 的字幕。Vidleaf 加入 AI 標點,保留原字詞與時間戳。下方可查看原始字幕。
AI 生成摘要
Tyrique White demonstrates a Python development workflow in Visual Studio Code. The tutorial separates the editor, its Python extension and the Python interpreter, then explains how virtual environments keep project dependencies apart. A small Flask sentiment API provides the running example for installing requirements, investigating unresolved imports and stepping through code with the debugger. The demonstration also shows how a saved launch configuration helps repeat a debugging session. Menu labels and installation details reflect the recorded version of the tools; the linked passages preserve the reasoning behind environment selection, dependency management and debugging a request.
重點
- [10:32] Virtual environments help projects avoid conflicts between different package requirements.
- [17:56] The Flask example connects unresolved imports with the selected environment and installed dependencies.
- [23:48] Sending a request to the running API lets the debugger inspect the code path and local values.
根據字幕生成。請透過連結對照原影片核對。 概覽與重點由 Vidleaf 於 2026-10-21 核對。
完整概覽
Tyrique White, a program manager for Python developer tools at Microsoft, demonstrates getting started with Python development in Visual Studio Code, describing VS Code as a free, open-source, cross-platform tool with an extension model and the Microsoft Python extension as the most popular extension on the VS Code marketplace, providing IntelliSense, debugging, testing, virtual environment and package support, formatters like Black and Autopep8, and linters like Pylint. He walks through downloading VS Code from code.visualstudio.com, installing the Python extension, and separately installing Python from python.org on Windows, via brew install on Mac, or the bundled version on Linux, then covers the File Explorer, source control, the Command Palette, IntelliSense, and the green Run button. He explains why virtual environments matter for avoiding package version conflicts, shows creating one with py -3 -m venv, and demonstrates a pre-created Flask app called Sentimentapi with a vstoolbox environment, where linting flags unresolved Flask and textblob imports until pip install -r requirements.txt is run. He then sets a breakpoint in the gutter on line 15 and starts the debugger via Debug > Start debugging or F5, selecting the Flask configuration. In the debugging segment, the presenter enters the path to the start-up file, tweaked to startup/app.py, and after hitting Enter the toolbar turns orange and the debug icon is highlighted white to denote debugging mode; for the API they navigate to the URL the app reports, reach the breakpoint in app.py by sending the word "Champion" to check its sentiment, see it in the locals variable window, step over with F10 and continue with F5, after which the API shows "Champion" as positive. The presenter reruns the app a different way, uses the debug icon's drop-down and Add configuration to create launch.json in the .vscode folder, notes that starting the debugger inside launch.json throws an error, and finds that testing "Wicked cool" from Boston returns positive. The segment also covers creating environments with different commands per OS, pip install -r requirements.txt, and the recommended bug-icon configuration flow, plus resources including the Getting Started tutorial at aka.ms/VST/pythonvscode, the Flask documentation, and the @PythonVSCode Twitter handle.
附時間戳的章節摘要
Python Development with VS Code
- [0:01] Tyrique demonstrates getting started with Python development using Visual Studio Code.
- [0:22] Tyrique White is a program manager for Python developer tools at Microsoft.
- [0:57] Python's sweet spot is being able to build apps quickly.
- [1:04] Tyrique will show a demo creating a very simple web application in nine lines of code.
- [1:12] Python is a scripting language for quick prototyping but is also used for production code.
Python Extension in VS Code Overview
- [1:24] The session focuses on Python and VS Code.
- [1:30] VS Code is a free, open-source, cross-platform tool that uses a popularized extension model for every language.
- [1:41] The session covers the Python extension in VS Code and the getting started experience.
- [1:45] Colleague Jeffrey will later discuss data science and VS Code for Python development in a separate episode recorded after this one.
- [2:04] The Python Extension offers features including IntelliSense, debugging, testing, support for virtual environment packages like VM and Conda, formatters like Black and Autopep8, and linters like pylints.
- [2:29] The Python extension is the most popular extension on the VS Code marketplace.
- [2:38] Visual Studio Code is now among the top three tools for Python developers.
- [2:51] Because it is an extension, users must install it separately after installing VS Code.
Downloading and Installing VS Code for Python
- [3:03] Visual Studio Code can be downloaded from code.visualstudio.com, which auto-detects the user's OS.
- [3:27] After opening VS Code, users see a welcome screen with customization tools and an activity bar for accessing installed extensions.
- [3:43] To use Python, users must install the Python extension from the Extensions tab by searching for Python.
- [4:02] The correct extension is the Microsoft Python extension marked with a green star, not the other Python extensions.
- [4:38] A third requirement is a separate installation of Python on the machine, since the Python extension for VS Code does not currently come with Python.
- [4:57] For Windows, the recommendation is to download the latest Python release from python.org; on Mac, a brew install works fine; on Linux, the bundled version typically works well.
VS Code Python Setup and Basics
- [5:26] The UI is a bit different to get on-boarded, so they start by getting people accustomed to it.
- [5:34] The File Explorer shows a tree view of an opened folder with its dependencies and files.
- [5:50] VS Code comes inbox with source control for connecting to favorite repos.
- [6:22] By default, installing VS Code on Windows adds VS Code to the path.
- [7:01] Typing code into the Command Prompt opens an instance of VS Code inside that folder.
- [7:15] On Linux and Mac you have to run a command to get VS Code on path.
- [7:29] To activate the Python extension you need to select a Python file (.py).
- [7:51] Clicking Python in the toolbar lists all the interpreters installed on the machine.
- [8:15] The extension can pick up most interpreters if they are in the standard places.
- [8:47] On Windows, Control Shift P brings up the Command Palette, where Python commands are denoted by Python colon.
- [9:21] Writing print immediately gives intellisense and documentation tool tips showing parameters.
- [9:47] A green Run button, added a couple of releases ago, runs the code and prints "Hello World" in the terminal.
Python Virtual Environments in VS Code
- [10:12] Virtual environments are an important Python development concept.
- [10:26] Two applications may each require separate versions of packages to function correctly.
- [10:32] Putting different package versions in the same environment may cause conflicts, broken code, or code that does not run.
- [10:45] Python developers are typically advised to create a virtual environment.
- [11:13] The command uses py -3 -m venv followed by a chosen name to create the environment from the command line.
- [11:46] VS Code prompts to select the new virtual environment for the workspace folder, adding its path to the .vscode folder.
- [12:25] The environment creates a local copy of Python for the workspace, including scripts and libraries, local to that workspace.
VS Code Python virtual environment demo
- [12:52] The speaker opens a pre-created Flask web app called Sentimentapi to demonstrate a virtual environment.
- [13:54] The project has a pre-created virtual environment named vstoolbox that persisted when reopened.
- [14:11] On Windows, a virtual environment can be activated from the command line via vstoolbox\scripts\activate, shown by the parentheses with the environment name.
- [14:45] Virtual environments can be shared with teammates or friends via source control, or you can just create a new one.
- [15:03] On Windows, control shift back tick opens a new terminal with the virtual environment already activated, avoiding the need to remember the activate path.
- [15:31] Yellow squiggles are linting messages; the Python extension asks to install a linter, with Pylint as the current default, and other tools like Flake8 are also supported.
- [16:01] The linter reports unresolved imports for Flask and textblob because those packages are not in the virtual environment, viewable under Problems.
- [16:39] The folder includes a requirements.txt listing all dependencies the application needs to function properly.
- [16:52] Depending on preferred source control setup, you can send up the virtual environments for others to download and activate, or send only requirements.txt so others create an environment and install packages.
- [17:23] Running pip install -r requirements.txt inside the activated environment installs the packages locally and the error messages go away.
Flask Sentiment API App Overview
- [17:56] While packages install, the application is explored.
- [18:02] It is a simple Flask app contained in a folder called Startup.
- [18:12] The homepage says "Python loves vstoolbox."
- [18:26] The sentimentapi folder name indicates the app lets you hit an endpoint with a word to determine its sentiment.
- [18:42] After packages finish installing, the problems go away and hovering over Flask and TextBlob shows documentation.
Building and Debugging a Sentiment API
- [19:01] The presenter runs the application using the debugger to also discuss debugging.
- [19:13] The sentiment API is described as simple, under perhaps 10 lines of code excluding comments and spaces.
- [19:33] Line 13 contains the route creating the endpoint that accepts a word and returns sentiment.
- [19:50] The module takes in the message and determines positive if sentiment score is greater than zero, negative if less than zero.
- [20:01] The presenter uses the textblob package, relying on IntelliSense and its documentation while typing.
- [20:28] If polarity is less than zero, the sentiment is reassigned to negative.
- [20:50] Textblob comes with sentiment built-in, so the presenter avoids reinventing the wheel.
- [21:18] The sentiment is returned on the web application page, and a breakpoint is set on line 15.
Starting the Python Debugger for a Flask App
- [21:36] Clicking to the left of the line number, in the gutter, sets a breakpoint.
- [21:41] The debugger feels familiar to those used to debugging, and is super easy to get started with for those who aren't.
- [21:50] The debugger can be started from the toolbar via Debug > Start debugging, or with the F5 keyboard shortcut.
- [22:10] Clicking Start debugging presents a debug configuration menu with a list of popular templates used by Python developers.
- [22:24] Because the app is a Flask app, Flask is selected from the configuration list.
- [22:31] Debugging a web app requires entering the path to the start-up file, tweaked here to startup/app.py.
- [22:52] After hitting Enter, debugging starts and the toolbar turns orange to denote debugging mode.
- [23:04] The debug icon becomes highlighted in white while debugging.
Debugging a Python API in VS Code
- [23:48] After hitting the breakpoint, navigate to the URL the app reports it is running on.
- [24:01] In VS Code you can reach that URL by copy-pasting it into a browser or by control-clicking.
- [24:12] The page shows the message "Python loves VS toolbox".
- [24:18] Because the breakpoint is set in app.py where the message is defined, hitting it requires sending a message to the API.
- [24:37] They send the word "Champion" to check its sentiment.
- [24:57] The blinking orange VS Code icon indicates the breakpoint inside the API was hit.
- [25:12] The debug menu shows the received message "Champion" in the locals variable window, alongside the watch window and call stack.
- [25:24] Step over with the debug toolbar or F10; all sentiments are set to positive first, then run through an if statement.
- [25:44] Continue with the arrow or F5, and the API then shows the word "Champion" as positive.
VS Code Debug Configuration and Sentiment Test
- [26:11] The presenter reruns the application a different way using the debugger.
- [26:21] The debug icon gives access to the configuration menu via a drop-down and Add configuration.
- [26:49] The configuration must be typed again because the previous configuration wasn't saved.
- [27:06] Navigating to Flask creates a file called launched.json, which is a JSON array of configurations.
- [27:39] The configuration is saved in the .vscode folder, created if it didn't exist before.
- [28:11] Starting the debugger while inside launch.json throws an error, so you must return to the file being debugged.
- [28:47] Testing 'Wicked cool' from Boston, where Wicked is negative and Cool is positive, the Text bot returns that it is positive.
- [29:43] Starting debugging from the toolbar does not save the configuration; use the debug icon and Add configuration to save it.
Python Environments and Debugging Setup
- [30:10] Creating new environments uses different commands depending on whether you are a Mac, Linux, or Windows user.
- [30:24] The pip install -r requirements.txt command installs a project's requirements if the project has such a file.
- [30:31] You can create a virtual environment first and then install the requirements listed there.
- [30:36] You can also install individual module names if you want to.
- [30:44] For debugging, the recommended approach is using the bug icon to open the debug configuration menu, then the drop-down, then App configuration to choose what to debug.
- [31:04] Putting the path to the web application creates a launch.json file.
- [31:09] You should not debug the launch.json file; instead go back to the Python file you are interested in and hit the green run button.
Python Learning Resources and VS Code
- [31:28] A Getting Started tutorial for Python in Visual Studio Code is available at aka.ms/VST/pythonvscode.
- [31:46] The getting started tutorial links out to other useful material, and the Flask documentation is cited as excellent for learning Flask.
- [32:01] Users can interact directly with the team via the Twitter handle @PythonVSCode, all one word.
- [32:11] The team is always interacting with users to understand which experiences work best and which don't.
- [32:20] The host calls the segment a great introduction to Visual Studio Code and to getting started with Python.
[0:00] >> 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.
[0:34] >> 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.
[1:07] 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.
[1:39] >> 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.
[2:10] 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.
[2:44] 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.
[3:17] 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.
[3:48] >> 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.
[4:21] 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.
[4:52] 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.
[5:23] 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.
[5:54] 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.
[6:30] 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.
[7:01] 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.
[7:33] 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?
[8:05] >> 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.
[8:35] 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.
[9:13] >> 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".
[9:43] >> 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.
[10:14] 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.
[10:45] 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.
[11:20] 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?"
[11:51] 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?
[12:22] >> 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?
[12:52] >> 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.
[13:29] 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".
[13:59] 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.
[14:33] >> 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.
[15:12] 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.
[15:44] 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.
[16:15] 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.
[16:50] 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.
[17:23] 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.
[17:56] 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.
[18:29] 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.
[19:01] 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.
[19:33] 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.
[20:06] 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.
[20:40] >> 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.
[21:15] >> 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.
[21:50] 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.
[22:21] 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.
[22:52] 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.
[23:32] >> 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.
[24:11] >> 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.
[24:43] 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".
[25:15] 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".
[25:53] 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.
[26:25] 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.
[26:59] 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.
[27:29] >> 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.
[28:08] 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.
[28:39] >> 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.
[29:16] >> 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.
[29:48] 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.
[30:36] 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.
[31:17] >> 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.
[32:00] >> 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.
[32:31] >> Thanks. >> Awesome. Thank you. >> Hope you guys enjoyed that, and we will see you next time on Visual Studio Toolbox. [MUSIC]
查看原始字幕
[0:00] >> 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.
[0:34] >> 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.
[1:07] 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.
[1:39] >> 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.
[2:10] 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.
[2:44] 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.
[3:17] 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.
[3:48] >> 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.
[4:21] 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.
[4:52] 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.
[5:23] 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.
[5:54] 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.
[6:30] 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.
[7:01] 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.
[7:33] 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?
[8:05] >> 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.
[8:35] 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.
[9:13] >> 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".
[9:43] >> 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.
[10:14] 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.
[10:45] 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.
[11:20] 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?"
[11:51] 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?
[12:22] >> 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?
[12:52] >> 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.
[13:29] 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".
[13:59] 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.
[14:33] >> 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.
[15:12] 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.
[15:44] 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.
[16:15] 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.
[16:50] 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.
[17:23] 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.
[17:56] 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.
[18:29] 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.
[19:01] 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.
[19:33] 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.
[20:06] 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.
[20:40] >> 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.
[21:15] >> 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.
[21:50] 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.
[22:21] 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.
[22:52] 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.
[23:32] >> 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.
[24:11] >> 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.
[24:43] 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".
[25:15] 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".
[25:53] 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.
[26:25] 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.
[26:59] 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.
[27:29] >> 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.
[28:08] 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.
[28:39] >> 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.
[29:16] >> 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.
[29:48] 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.
[30:36] 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.
[31:17] >> 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.
[32:00] >> 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.
[32:31] >> Thanks. >> Awesome. Thank you. >> Hope you guys enjoyed that and we will see you next time on Visual Studio Toolbox. [MUSIC]
在 Vidleaf 工作台開啟
檢索字幕、選取句子、複製附時間戳的引用,或按需翻譯。
來源署名
「Getting Started with Python in Visual Studio Code」由 Microsoft Visual Studio 發布,採用 CC BY 3.0 授權。來源影片:https://www.youtube.com/watch?v=7EXd4_ttIuw。此頁呈現影片的文字字幕,由 Vidleaf 加入段落與時間戳。創作者與 Vidleaf 無隸屬或背書關係。頁面若提供 AI 標點、名稱修正或摘要,會分開標示,原始字幕仍可查看。
若你是創作者或權利人,可申請更正或下架: copyright@vidleaf.app (參閱 關於這些頁面)。
最後更新