文字记录 · CC BY 3.0

在Visual Studio Code中开始使用Python

本页标题、字幕、摘要和阅读笔记为已保存的 AI 译文,可能有误。时间戳与来源一致,可展开查看原始字幕,并对照原视频核对。

Tyrique White在VS Code中设置Python:Python扩展、解释器和虚拟环境、一个使用TextBlob的小型Flask情感API,以及调试。

Microsoft Visual Studio · 发布于 · 33分钟 · 英语 · 许可证:CC BY 3.0 · 来源:在YouTube上观看

文字记录来源:YouTube上创作者上传的字幕。Vidleaf添加了AI标点;保留原始措辞和时间戳锚点。原始文字记录可在下方获取。

AI生成的摘要

Tyrique White在Visual Studio Code中演示Python开发工作流程。本教程将编辑器、其Python扩展和Python解释器分开讲解,然后解释虚拟环境如何将项目依赖分开。一个小型Flask情感API提供了安装需求、调查未解析导入以及使用调试器逐步执行代码的运行示例。演示还展示了保存的启动配置如何帮助重复调试会话。菜单标签和安装细节反映了工具的录制版本;链接的段落保留了环境选择、依赖管理和调试请求背后的推理。

要点

  • [10:32] 虚拟环境帮助项目避免不同包需求之间的冲突。
  • [17:56] Flask示例将未解析的导入与所选环境和已安装的依赖联系起来。
  • [23:48] 向正在运行的API发送请求可以让调试器检查代码路径和局部值。

根据文字记录生成。请对照原始视频核查所链接的段落。简要说明和要点由 Vidleaf 于 2026-10-21 审核。

完整概述

Tyrique White是微软Python开发者工具的项目经理,他演示了在Visual Studio Code中开始Python开发,将VS Code描述为一个免费、开源、跨平台的工具,具有扩展模型,而微软Python扩展是VS Code市场上最受欢迎的扩展,提供IntelliSense、调试、测试、虚拟环境和包支持,以及Black和Autopep8等格式化工具和Pylint等linter。他逐步演示了从code.visualstudio.com下载VS Code、安装Python扩展,并单独从python.org在Windows上安装Python、在Mac上通过brew install安装、或在Linux上使用捆绑版本,然后介绍了文件资源管理器、源代码管理、命令面板、IntelliSense和绿色运行按钮。他解释了为什么虚拟环境对于避免包版本冲突很重要,展示了使用py -3 -m venv创建虚拟环境,并演示了一个预先创建的名为Sentimentapi的Flask应用,带有vstoolbox环境,其中linting标记未解析的Flask和textblob导入,直到运行pip install -r requirements.txt。然后他在第15行的边栏设置断点,并通过调试>开始调试或F5启动调试器,选择Flask配置。在调试部分,演示者输入启动文件的路径,调整为startup/app.py,按下Enter后工具栏变为橙色,调试图标高亮为白色以表示调试模式;对于API,他们导航到应用报告的URL,通过发送单词“Champion”来检查其情感,到达app.py中的断点,在局部变量窗口中看到它,使用F10单步跳过,并使用F5继续,之后API显示“Champion”为正面。演示者以另一种方式重新运行应用,使用调试图标的下拉菜单和添加配置在.vscode文件夹中创建launch.json,指出在launch.json内部启动调试器会抛出错误,并发现从波士顿测试“Wicked cool”返回正面。该部分还涵盖了根据不同操作系统使用不同命令创建环境、pip install -r requirements.txt以及推荐的错误图标配置流程,以及资源,包括aka.ms/VST/pythonvscode上的入门教程、Flask文档和@PythonVSCode Twitter账号。

带时间戳的章节摘要

使用VS Code进行Python开发

  • [0:01] Tyrique演示了使用Visual Studio Code开始Python开发。
  • [0:22] Tyrique White是微软Python开发者工具的项目经理。
  • [0:57] Python的优势在于能够快速构建应用。
  • [1:04] Tyrique 将演示用九行代码创建一个非常简单的 Web 应用程序。
  • [1:12] Python 是一种用于快速原型设计的脚本语言,但也用于生产代码。

VS Code 中的 Python 扩展概述

  • [1:24] 本次会议重点介绍 Python 和 VS Code。
  • [1:30] VS Code 是一款免费、开源、跨平台的工具,它为每种语言都采用了广受欢迎的扩展模型。
  • [1:41] 本次会议涵盖 VS Code 中的 Python 扩展以及入门体验。
  • [1:45] 同事 Jeffrey 稍后将在本次会议之后录制的另一期节目中讨论数据科学以及使用 VS Code 进行 Python 开发。
  • [2:04] Python 扩展提供的功能包括 IntelliSense、调试、测试、对 VM 和 Conda 等虚拟环境包的支持、Black 和 Autopep8 等格式化工具,以及 pylints 等 linter。
  • [2:29] Python 扩展是 VS Code 市场上最受欢迎的扩展。
  • [2:38] Visual Studio Code 现在已跻身 Python 开发者的三大工具之列。
  • [2:51] 由于它是一个扩展,用户必须在安装 VS Code 后单独安装它。

为 Python 下载并安装 VS Code

  • [3:03] Visual Studio Code 可以从 code.visualstudio.com 下载,该网站会自动检测用户的操作系统。
  • [3:27] 打开 VS Code 后,用户会看到一个带有自定义工具的欢迎屏幕,以及一个用于访问已安装扩展的活动栏。
  • [3:43] 要使用 Python,用户必须从“扩展”选项卡中搜索 Python 来安装 Python 扩展。
  • [4:02] 正确的扩展是带有绿色星标的 Microsoft Python 扩展,而不是其他 Python 扩展。
  • [4:38] 第三个要求是在机器上单独安装 Python,因为 VS Code 的 Python 扩展目前并不附带 Python。
  • [4:57] 对于 Windows,建议从 python.org 下载最新的 Python 版本;在 Mac 上,brew install 即可正常工作;在 Linux 上,捆绑版本通常可以很好地工作。

VS Code Python 设置与基础

  • [5:26] 用户界面略有不同,以便于上手,因此他们首先让人们熟悉它。
  • [5:34] 文件资源管理器以树状视图显示已打开的文件夹及其依赖项和文件。
  • [5:50] VS Code 内置了源代码管理功能,可连接到常用的代码仓库。
  • [6:22] 默认情况下,在 Windows 上安装 VS Code 会将 VS Code 添加到路径中。
  • [7:01] 在命令提示符中输入代码会在该文件夹内打开一个 VS Code 实例。
  • [7:15] 在 Linux 和 Mac 上,你必须运行一条命令才能将 VS Code 添加到路径中。
  • [7:29] 要激活 Python 扩展,你需要选择一个 Python 文件(.py)。
  • [7:51] 点击工具栏中的 Python 会列出机器上安装的所有解释器。
  • [8:15] 如果大多数解释器位于标准位置,该扩展可以识别它们。
  • [8:47] 在 Windows 上,按 Control Shift P 会调出命令面板,其中 Python 命令以 Python 冒号表示。
  • [9:21] 输入 print 会立即给出 IntelliSense 和显示参数的文档工具提示。
  • [9:47] 几个版本之前添加的绿色“运行”按钮会运行代码并在终端中打印“Hello World”。

VS Code 中的 Python 虚拟环境

  • [10:12] 虚拟环境是 Python 开发中的一个重要概念。
  • [10:26] 两个应用程序可能各自需要不同版本的包才能正常运行。
  • [10:32] 将不同版本的包放在同一环境中可能会导致冲突、代码损坏或代码无法运行。
  • [10:45] 通常建议 Python 开发者创建一个虚拟环境。
  • [11:13] 该命令使用 py -3 -m venv 后跟所选名称,从命令行创建环境。
  • [11:46] VS Code 会提示为工作区文件夹选择新的虚拟环境,并将其路径添加到 .vscode 文件夹中。
  • [12:25] 该环境会为工作区创建一份本地 Python 副本,包括脚本和库,均位于该工作区本地。

VS Code Python 虚拟环境演示

  • [12:52] 演讲者打开了一个预先创建的名为 Sentimentapi 的 Flask Web 应用,以演示虚拟环境。
  • [13:54] 该项目有一个预先创建的名为 vstoolbox 的虚拟环境,在重新打开时仍然存在。
  • [14:11] 在 Windows 上,可以通过 vstoolbox\scripts\activate 从命令行激活虚拟环境,括号中会显示环境名称。
  • [14:45] 虚拟环境可以通过源代码管理与队友或朋友共享,或者你也可以直接创建一个新的。
  • [15:03] 在 Windows 上,按 control shift 反引号会打开一个已激活虚拟环境的新终端,从而无需记住激活路径。
  • [15:31] 黄色波浪线是 linting 消息;Python 扩展会提示安装 linter,目前默认是 Pylint,同时也支持 Flake8 等其他工具。
  • [16:01] linter 报告 Flask 和 textblob 的导入未解析,因为这些包不在虚拟环境中,可以在“问题”下查看。
  • [16:39] 该文件夹包含一个 requirements.txt,列出了应用程序正常运行所需的所有依赖项。
  • [16:52] 根据偏好的源代码管理设置,你可以上传虚拟环境供他人下载并激活,或者只发送 requirements.txt,让他人创建环境并安装包。
  • [17:23] 在已激活的环境中运行 pip install -r requirements.txt 会在本地安装这些包,错误消息就会消失。

Flask Sentiment API 应用概述

  • [17:56] 在安装包的同时,对应用程序进行了探索。
  • [18:02] 这是一个简单的 Flask 应用,包含在一个名为 Startup 的文件夹中。
  • [18:12] 主页上写着“Python loves vstoolbox。”
  • [18:26] sentimentapi 文件夹名称表明,该应用允许你通过一个单词访问端点以确定其情感。
  • [18:42] 软件包安装完成后,问题消失,悬停在 Flask 和 TextBlob 上会显示文档。

构建和调试情感 API

  • [19:01] 演讲者使用调试器运行应用程序,同时也讨论调试。
  • [19:13] 情感 API 被描述为简单,代码行数可能不到 10 行,不包括注释和空格。
  • [19:33] 第 13 行包含创建端点的路由,该端点接受一个单词并返回情感。
  • [19:50] 该模块接收消息,如果情感得分大于零则确定为正面,如果小于零则确定为负面。
  • [20:01] 演讲者使用 textblob 包,在输入时依赖 IntelliSense 及其文档。
  • [20:28] 如果极性小于零,情感被重新赋值为负面。
  • [20:50] Textblob 内置了情感分析,因此演讲者避免重新发明轮子。
  • [21:18] 情感在 Web 应用程序页面上返回,并在第 15 行设置了一个断点。

为 Flask 应用启动 Python 调试器

  • [21:36] 单击行号左侧的装订线区域即可设置断点。
  • [21:41] 对于习惯调试的人来说,调试器感觉很熟悉,对于不习惯的人来说,上手也超级容易。
  • [21:50] 可以从工具栏通过“调试”>“开始调试”启动调试器,或使用 F5 键盘快捷键。
  • [22:10] 单击“开始调试”会显示一个调试配置菜单,其中包含 Python 开发者使用的流行模板列表。
  • [22:24] 由于该应用是 Flask 应用,因此从配置列表中选择 Flask。
  • [22:31] 调试 Web 应用需要输入启动文件的路径,此处调整为 startup/app.py。
  • [22:52] 按下 Enter 后,调试开始,工具栏变为橙色以表示调试模式。
  • [23:04] 调试时,调试图标变为白色高亮。

在 VS Code 中调试 Python API

  • [23:48] 命中断点后,导航到应用报告其运行所在的 URL。
  • [24:01] 在 VS Code 中,你可以通过将 URL 复制粘贴到浏览器中或按住 Control 键单击来访问该 URL。
  • [24:12] 页面显示消息“Python loves VS toolbox”。
  • [24:18] 由于断点设置在定义消息的 app.py 中,要命中它需要向 API 发送消息。
  • [24:37] 他们发送单词“Champion”以检查其情感。
  • [24:57] 闪烁的橙色 VS Code 图标表示 API 内部的断点已命中。
  • [25:12] 调试菜单在局部变量窗口中显示收到的消息“Champion”,同时还有监视窗口和调用堆栈。
  • [25:24] 使用调试工具栏或 F10 单步跳过;所有情感首先设置为正面,然后通过 if 语句运行。
  • [25:44] 使用箭头或 F5 继续,然后 API 将单词“Champion”显示为正面。

VS Code 调试配置和情感测试

  • [26:11] 演讲者使用调试器以另一种方式重新运行应用程序。
  • [26:21] 调试图标通过下拉菜单和“添加配置”提供对配置菜单的访问。
  • [26:49] 必须再次输入配置,因为之前的配置未保存。
  • [27:06] 导航到 Flask 会创建一个名为 launched.json 的文件,它是一个配置的 JSON 数组。
  • [27:39] 配置保存在 .vscode 文件夹中,如果之前不存在则创建该文件夹。
  • [28:11] 在 launch.json 内部启动调试器会抛出错误,因此你必须返回到正在调试的文件。
  • [28:47] 测试来自波士顿的“Wicked cool”,其中 Wicked 是负面而 Cool 是正面,Text bot 返回它是正面的。
  • [29:43] 从工具栏启动调试不会保存配置;使用调试图标和“添加配置”来保存它。

Python 环境和调试设置

  • [30:10] 创建新环境使用不同的命令,取决于你是 Mac、Linux 还是 Windows 用户。
  • [30:24] 如果项目有 requirements.txt 文件,pip install -r requirements.txt 命令会安装项目的需求。
  • [30:31] 你可以先创建虚拟环境,然后安装其中列出的需求。
  • [30:36] 如果需要,你也可以安装单独的模块名称。
  • [30:44] 对于调试,推荐的方法是使用 bug 图标打开调试配置菜单,然后使用下拉菜单,再使用应用配置来选择要调试的内容。
  • [31:04] 输入 Web 应用程序的路径会创建一个 launch.json 文件。
  • [31:09] 你不应该调试 launch.json 文件;而是返回到你感兴趣的 Python 文件并点击绿色的运行按钮。

Python 学习资源和 VS Code

  • [31:28] 适用于 Visual Studio Code 中 Python 的入门教程可在 aka.ms/VST/pythonvscode 获取。
  • [31:46] 入门教程链接到其他有用的材料,Flask 文档被引用为学习 Flask 的优秀资源。
  • [32:01] 用户可以通过 Twitter 账号 @PythonVSCode(全部连写)直接与团队互动。
  • [32:11] 团队始终与用户互动,以了解哪些体验效果最好,哪些不好。
  • [32:20] 主持人称该片段是对 Visual Studio Code 和 Python 入门的绝佳介绍。

[0:00] >> 今天的 Visual Studio 工具箱:Tyrique 将向我们展示如何使用 Visual Studio Code 开始 Python 开发。[音乐] >> 嗨,欢迎来到 Visual Studio 工具箱。我是主持人 Robert Green,今天和我一起的是 Tyrique White。嗨,Tyrique。>> 嗨 Robert,感谢邀请我。>> 感谢你来参加节目。>> 大家好,我是 Tyrique White,是微软 Python 开发者工具的项目经理。>> 酷。所以我们要谈谈做 Python,我们要展示 Visual Studio Code。

[0:34] >> 对。>> 太棒了。那么请再给我们简要介绍一下,因为我想我们之前谈过,但请简要说说 Python 的最佳适用场景。我是 C# 开发者,或者 JavaScript 开发者,我应该在什么时候、为什么学习 Python?最常见的场景有哪些?>> 当然。Robert,这是个好问题,我们在会议上经常被问到。Python 的最佳适用场景是能够快速构建应用。稍后我的一个演示中,我将展示用九行代码就能创建。

[1:07] 一个非常简单的 Web 应用。我仍然创建了一个 Web 应用。这是 Python 的基础。它是抓取语言,人们能够快速制作原型。但它也用于生产代码,这非常令人兴奋。>> 好的。>> 是的,酷。>> 是的,太棒了。所以今天具体来说,我们要谈谈 Python 和 VS Code。对于不了解的人,VS Code 是一个免费、开源、跨平台的工具,它为每种语言使用流行的扩展模型。

[1:39] >> 对。>> 所以今天我们要谈谈 VS Code 中的 Python 扩展和入门体验。然后稍后,我的同事 Jeffrey 将谈谈数据科学和用于 Python 开发的 VS Code。>> 那将是我们在这次之后录制的另一集。>> 当然。>> 向人们展示演播室幕后的运作方式。>> 对。>> 这样我就穿着同样的衬衫。>> 太棒了,酷。那么让我们开始吧。所以 Python 扩展提供了许多非常丰富的功能。

[2:10] 它提供 IntelliSense、调试、测试,支持像 VM 和 Conda 这样的虚拟环境包,还有一些格式化工具,比如 Black 和 Autopep8,以及一些流行的 linter,比如 pylints。关于 VS Code 中 Python 扩展的一些有趣事实:它是 VS Code 市场上最受欢迎的扩展。>> 真的,比 C-sharp 还确定。>> 是的。>> 这很有趣。>> 更棒的是,Visual Studio Code 现在已成为 Python 开发者排名前三的工具,这真的令人兴奋。

[2:44] 围绕 Python 有很多炒作,希望到最后你们能更理解一些原因。就是这样。>> 所以它是一个扩展,这意味着你必须去获取它?>> 是的,没错。>> 你安装了 VS Code,你并没有这个 Python 扩展。>> 对。所以是的。让我们退一步。让我们向你展示如何下载和安装 Visual Studio Code。如果你还没有 Visual Studio Code,获取它的链接是 code.visualstudio.com,你来到这个外部和下载页面。

[3:17] 如你所见,它会自动检测我正在使用哪个操作系统。>> 好的。>> 我可以快速获取一份 VS Code。我已经安装了它,但我相信我们明白那里的意思。一旦你有了 Visual Studio Code,你就会打开它,然后你会看到这个欢迎屏幕。这个欢迎屏幕有允许你自定义的工具。这里有这个很棒的活动栏,它让你可以访问你已安装的所有扩展。所以在你下载 Visual Studio Code 并想使用 Python 之后,你必须去获取 Python 扩展。

[3:48] >> 好的。>> 所以要做到这一点,你要去这里的扩展批处理,然后我要输入 Python。因为它是最受欢迎的,它会直接出现在最上面,你会看到 Microsoft 的 Python 扩展。>> 不是 Python for VS Code 或 Python 扩展?>> 不。你要的是这里有绿色星标的那个。好问题。>> 你参与开发的那个。>> 我参与开发的那个。>> 不是说其他那些有什么问题,但这是你参与开发的那个?>> 绝对是。如你所见,我已经安装了这个。

[4:21] 所以它显示卸载,我不会这么做,但会有一个可以点击的安装徽章。你还可以看到我们这里有一些关于如何开始的快速提示,差不多就是我今天要讲的内容。所以你需要安装 VS Code,你已经完成了。你需要安装扩展,我已经完成了。然后你需要第三样东西,就是在你的机器上安装 Python。>> 对。>> 是的。我们发现的一个误解是,有些人常常认为 VS Code 的 Python 扩展自带 Python,但目前并非如此。

[4:52] 所以绕过这一点的一种方法取决于你使用哪个操作系统。对于 Windows,我们建议去 python.org,我这里就有,下载最新版本的 Python;在 Mac 上,brew install 就可以;对于 Linux,通常 Linux 自带的 Python 版本就可以很好地工作,你能获得 Python 扩展的所有这些功能。>> 明白了。>> 听起来不错。>> 好的。太棒了。>> 所以现在我们有了代码,有了扩展,有了 Python。>> 是的,我有 python。

[5:23] 完美。好的。那么让我们开始吧。让我们让人们熟悉这里的 UI,因为上手确实有点不同。>> 好的。>> 所以现在我已经有了扩展。如你所见,我这里有文件资源管理器。当我打开一个文件夹时,我将能够看到该文件夹的树状视图,看到那里的所有依赖项和所有文件。我有一个调试器图标,我们稍后会讨论。我还有很多其他东西。VS Code 开箱即带源代码管理。

[5:54] 所以我可以使用我最喜欢的仓库,连接到那些仓库,并做所有我熟悉和喜爱的事情。我们这里还有工具栏,让你可以看到。我目前登录了 Azure。我安装了 life share 扩展。我这里有我的工具栏,所有那些好东西。>> 好的。>> 那么我现在想做的是创建一个文件夹,让我们开始玩一些代码。听起来怎么样?>> 听起来很棒。>> 另一件很酷的事情是,默认情况下,当你在 Windows 上安装 VS Code 时,它会把 VS Code 添加到路径中。

[6:30] 我会向你确切展示那意味着什么。所以我想做的是导航到我的命令提示符,然后我要找到我这里有的 Demos 文件夹。我要为我们创建一个新文件夹。我要把它叫做 VS toolbox。我要导航到那个文件夹。现在真正酷的是,因为我的 Windows 机器上 VS Code 在路径中,我所要做的就是输入 code。

[7:01] 在命令提示符中输入,它就会在那个文件夹内打开一个 VS Code 实例。>> 好的。>> 你可以从我这里的文件资源管理器看到。>> 所以你可以从 code 打开文件夹,你可以转到该文件夹并从该位置启动 code?>> 没错。在 Linux 和 Mac 上,你必须运行一个命令才能让 VS Code 进入路径,但如果你专注于命令行,这非常方便。所以现在我要实际做的是创建一个新文件。这是激活 Python 扩展的重要部分。

[7:33] 为了让扩展激活——你可以在下面看到“正在激活扩展”——你需要选择一个 Python 文件夹。抱歉,应该说是一个 Python 文件。>> 好的。>> .Py 是文件类型。如你所见,我马上就能验证扩展是否已激活,我的工具栏下方这里确实有 Python。如果我点击它,它会列出我机器上安装的所有不同的 Python 解释器。>> 你安装 Python 的时候这样没问题吧?

[8:05] >> 嗯,我出于不同原因安装了多个不同版本;为了测试,为了随便玩玩。但这些就是我拥有的所有解释器版本。只要它们位于标准位置——安装 Python 时通常就在这些位置——扩展就能识别出其中大部分。所以这挺酷的。那么我们创建了文件。所以现在我想做的是谈谈命令面板。

[8:35] 这对我们来说会非常有用,因为对于更偏向命令行驱动的开发者来说,你会希望在编辑器内部获得终端体验。所以,要打开我所说的命令面板,在 Windows 机器上你要按 Control Shift P 来调出命令面板。再说一次,命令面板里有你所能想到并喜爱的所有 Python 命令。所有那些 Python 命令都以 Python 冒号标示。>> 好的。>> 你可以滚动整个列表来获取任何你想要的内容。我要找的是创建终端,因为我想调出一个终端,用来做一些我现在要讲的命令行驱动的事情。

[9:13] >> 好的。>> 酷。所以这里真正酷的是,我可以开始编辑或开始写一些代码。所以如果我输入 print,你可以看到我立刻开始获得我们所说的 IntelliSense 以及一些文档。比方说我不熟悉 print 语句,我可以把鼠标移到这里的文档工具提示上,就能确切看到它接受哪些参数。这里我只想打印“Hello world”。

[9:43] >> 好的。>> 酷。要运行这个,这其实是我们几个版本前刚添加的功能,就是绿色的“运行”按钮。所以我可以直接用这个按钮运行我的代码,它会在终端中打印出“Hello World”。>> 酷。>> 我们经典的编码示例,“Hello world”。>> 好的。>> 完美。这很酷。现在,我接下来想谈的、对 Python 开发真正重要的东西,是虚拟环境的概念。

[10:14] 你熟悉虚拟环境吗,Router?>> 不太熟悉,不熟。>> 好的。那我简要解释一下它们是什么。在 Python 中,你可以有应用 A 和应用 B。比方说我是一名开发者,我正在开发两个不同的应用。然而,每个应用都需要不同版本的包才能正常运行。>> 好的。>> 如果我把所有这些不同的包版本都包含在同一个应用或同一个环境中,就有可能发生冲突。有些代码可能无法运行,有些东西可能会被破坏。

[10:45] 所以通常我们建议 Python 开发者做的是创建一个虚拟环境,我现在就向你展示如何操作。酷。那么回到我的命令行这里。我在我的 VS toolbox 文件夹中。我有我的 hello.Py 文件,print“Hello world”,一切正常。所以根据你使用的操作系统,命令会有所不同。我实际上会先向你展示我们在那些命令上的操作。然后我们也可以看看 Linux、Mac 的命令。但为了运行、从命令行创建虚拟环境,我要做的是输入 Py,是 Python 的缩写,-3 表示我要使用的 Python 版本。

[11:20] 短横线 m,意思是,好的,我要传入一个我想让你使用的模块。这个模块叫做 VM,是 virtual environment(虚拟环境)的缩写,这很合理。我实际上要把我们的虚拟环境叫做……随便给我起个名字。你最喜欢的文件夹名字是什么?>> 我最喜欢的文件夹名字,pictures。>> Pictures。酷。>> 真正酷的是,你注意到弹出的这个提示。它说:“我们注意到已创建一个新的虚拟环境,是否要为工作区文件夹选择它?”

[11:51] 真正酷的是,如果我选择“是”并导航到我的 .vscode 文件夹,你会看到我们加入了这一行,其中包含指向那个 pictures 虚拟环境的路径。所以下次我打开这个文件夹时,我就已经在这个虚拟环境里了——你可以看到这里标示为 pictures 虚拟环境——这样我下次就能更快地提高效率。>> 那不是硬盘上的实际文件夹,对吧?

[12:22] >> 所以它会创建一个本地副本,这其实是个好问题。它会为这个工作区创建一个 Python 的本地副本,你可以看到它包含的脚本列表,以及该虚拟环境中可能拥有的所有库。但再说一次,它只限于这个工作区本地,这样你的应用就能获得该虚拟环境内的所有内容。所以我实际上……>> 它会一直保留吗?如果我退出代码再回来,我还能访问那个环境,还是必须重新创建它?

[12:52] >> 没错,你可以访问这个环境。所以我现在实际要做的是,“Hello world”虽然很令人兴奋,但我其实想给你展示一个我已经创建好虚拟环境的 Web 应用。>> 好的。酷。>> 那我们现在就来回答你的问题。为了关闭这个,我要去“文件”、“关闭文件夹”。有几种方法可以打开一个新文件夹。我可以在这里去“文件”、“打开文件夹”,然后在文件资源管理器里导航。还有开始菜单可以让我打开文件夹,但实际上我想要的演示是“Sentimentapi”,它就在我的最近项目里。

[13:29] 那为什么不直接在那里点击它呢?酷。所以关于这个应用,有几件事需要立刻注意,它是一个简单的 Web 应用,使用了流行的 Python 包 flask,这个包就是用来创建简单 Web 应用的,而为了回答你的问题 Robert,我这里有一个名为“vstoolbox”的虚拟环境。

[13:59] 再说一次,这是我预先创建好的,所以当我重新打开它时它就被保存下来了。更好的是,我实际上可以在终端里用两种方式激活那个虚拟环境。所以如果你习惯用命令行,在 Windows 机器上激活虚拟环境的方式是,你可以从虚拟环境名称开始,所以是 vstoolbox 反斜杠,因为我们在 Windows 上,然后是 scripts 反斜杠 activate,你会看到虚拟环境已激活,表现为带虚拟环境名称的括号。

[14:33] >> 那么这些信息会存储在源代码管理中吗?所以如果我拿这个应用,把它放到 GitHub 上,回家,克隆它,并在本地拉取下来,我不仅拥有代码,还拥有环境信息吗?>> 是的,绝对可以。如果你想与团队中的开发者或朋友共享虚拟环境,你完全可以这样做。我也会给你展示,或者你可以直接创建一个新的,但我会给你展示。我们之前稍微谈过这个,但我想它很快就会完整地串起来。另一种快速激活这个虚拟环境的方法,如果你不想记住 activate 文件的路径,在 Windows 机器上你可以按 control shift 反引号。

[15:12] 它会为你打开一个新的终端,并且这个虚拟环境已经激活。所以这里有一些非常酷的东西。让我们探索一下编辑器。所以你们或者每个人可能都注意到了这里出现的黄色波浪线。这些黄色波浪线就是我们所说的 linting 消息,所以重要的是要知道,第一次安装 Python 扩展时,它会询问你是否要安装一个 linter。

[15:44] 目前的默认是 Pylint,所以它会询问你是否想使用 Pylint,但我们也支持其他 linting 工具,比如 Flake8 等。所以你在这里看到的就是这些 linting 消息。所以如果我把鼠标悬停在这些 linting 消息上,它会告诉我,它在说:“嘿,我找不到未解析的导入 text blob。”所以这基本上意味着,它无法在我的虚拟环境中找到这个包。

[16:15] 所以它给了我一个错误,这非常有用,因为它给了我一个代码诊断或问题,我也可以在这里的“问题”下查看,你会看到我有问题列表,未解析的导入 Flask,未解析的导入 textblob。所以我们实际上可以做的是安装它们。与上次我们创建的文件夹相比,这个文件夹还有一个不同之处,就是我这里有这个 requirements.txt 文件,对于不了解的人来说,对于 Python 开发者来说,这只是我的应用程序正常运行所需的所有依赖项的列表。

[16:50] 所以回到你最初的观点。取决于你喜欢的设置类型,哪种源代码管理,你可能会上传你的虚拟环境,这样人们可以下载它并直接激活它,或者你可能只上传 requirements.txt 文件,让人们先创建一个虚拟环境,然后在这里安装所有包。所以我要做的是,我将从这个 requirements.txt 文件安装那些包,所以回到我的命令行,再次注意我仍然在这个虚拟环境里面,因为我希望所有这些包都本地包含在这个应用程序中。

[17:23] 所以我要执行 pip install -r,然后是 requirements.txt,然后按“Enter”。你会注意到的是,它会找到那些包并为我安装它们。它正在获取列表,我之前下载过这些,所以它只是使用这些包的缓存版本。重要的是要注意,当它安装这些时,你会注意到这些错误消息会消失,这真的很酷。

[17:56] 所以在它安装那些包的时候,让我们探索一下这个应用程序。所以这就是,正如我之前说的,简单的 Flask 应用。所以我在这里做的是,我本质上是在创建一个包含在这个名为 Startup 的文件夹中的 Flask 应用。我这里有一个简单的首页,它只是说:“Python 爱 vstoolbox”,这实际上是一个非常酷的应用程序,因为它不仅是一个简单的 Web 应用,而且 sentimentapi 是文件夹的标题,表示。

[18:29] 所以这将允许我们做的是,它将允许我们用一个词访问一个端点,这个 API 将帮助我们确定那个词的情感,因为也许我只是需要帮助确定词语具有什么样的情感。所以非常酷,它为我们完成了包的安装,注意我的问题消失了,突然如果我悬停在 Flask 上,我就能得到它的文档。Text blob 我得到模块 text blob,这真的很酷。所以现在我想让我们做的是探索这个应用程序。

[19:01] 那么让我们实际运行这个应用程序。有几种运行方式。我想使用调试器来运行它,因为我也想借此机会谈谈调试以及它能带来什么。正如我之前所说,这是一个情感 API,非常简单,如果去掉所有注释和空格,代码可能不到 10 行,这再次体现了 Python 的美妙之处——你之前提到的,不到 20 行代码,本质上,我就能创建一个强大的情感 API。实际上我自己还有一些待办事项,我需要实际构建这个 API。

[19:33] 那么如果我向下滚动到第 13 行,这实际上是创建那个端点的路由,允许我们发送一个词并获取情感结果。所以我想做的是,这个模块的工作方式是,它会接收那条消息,如果情感分数大于零,我们就判定为正面。如果小于零,就是负面。所以我要做的是,我实际上要把它写出来。所以如果 textblob 我使用那个包,注意我在这里得到了 IntelliSense。

[20:06] 所以我要按 Tab 键接受那条消息。再次,这就是我想要的。所以如果消息有情感,注意再次这个带有文档的 IntelliSense,让我在输入时学习。polarity 小于零,那么我将重新赋值那个情感,我会说,“好的,你是负面的。” 尝试同时打字,让我们去掉这里的注释。

[20:40] >> 那么 textblob 怎么会有 sentiment 属性呢?>> 抱歉?>> Textblob.sentiment。>> 哦,这就是 textblob 包的美妙之处。>> 真的,它内置了情感分析?>> 是的。>> 那很酷。>> 这是 Python 的另一个酷炫之处,我完全可以从头创建自己的情感 API,如果我想的话,但为什么要重新发明轮子呢?>> 因为我从未见过我用过的文本框内置了那个功能。>> 对。所以它实际上做的是将那条消息传递给 textblob 模块,然后在底层做一些事情,然后得到酷炫的情感极性等等。

[21:15] >> 酷。>> 所以我们重新赋值情感为负面,然后我实际上要做的是,我将把那个情感返回到页面上,在 Web 应用程序页面上,你稍后会看到。所以酷。让我们运行应用程序。>> 好的。>> 我将使用调试器来运行它,因为有几件事我想谈谈。那么让我们在第 15 行设置一个断点,注意我点击了行号左侧,这被称为装订线。对于那些习惯调试的人来说,这会有同样的感觉,对于那些不习惯的人来说,不要害怕,开始使用调试器超级简单。

[21:50] 那么要开始这个,有几种方式可以运行我的调试器,我将折叠那个,以便我能全神贯注地给你。我可以使用工具栏,点击“调试”然后点击“开始调试”。它也有键盘快捷键 F5,所以我要开始这样做。注意当我点击“开始调试”时,它会呈现调试配置菜单。我们有一个 Python 开发者使用的流行模板列表。

[22:21] 还记得我之前提到这是一个 Flask 应用吗?>> 是的。>> 所以你可以看到 Flask 在那个配置列表中,这就是我想要的,我将选择它。调试 Web 应用的下一步是输入我的启动文件的路径。记住我说过这个启动文件包含在一个名为 Startup 的文件夹中,所以我实际上要做的是稍微调整这个路径,以实际指向我的启动文件,它创建了我的 Flask 应用,即 startup/app.py。

[22:52] 一旦我按下“回车”,它就会开始为我调试,注意这里的工具栏变成橙色,表示我处于调试模式。另一个酷炫的事情是这里的调试图标变成白色高亮,所以你可以看到我也在调试。>> 我想它确实如此。>> 它确实如此,是的。>> 好的。>> 我从未注意到那个。>> 是的。这是一个非常酷的小东西。>> 但同时,你还记得吗,我从来不会注意到这些事情,你还记得 Visual Studio 中顶部菜单选项全是大写的巨大争议吗,那是一个巨大的争议。

[23:32] >> 那实际上早于我在微软的时间。>> 不管怎样,我发现那个是因为有争议。>> 不,是的。>> 关于我的事说得够多了,让我们回到应用。>> 是的。所以调试 Web 应用,另一个刚开始使用 Web 应用的人可能不知道的事情是,为了命中断点,我在第 15 行设置的那个,我实际上需要导航到它在这里给我的 URL。>> 好的。>> 所以它告诉我它运行在这个 URL 上,真正酷的是,在 VS Code 内部,为了到达那个 URL,我可以要么复制粘贴到我的浏览器中,要么我可以直接按住 Ctrl 点击,我将这样做。

[24:11] >> 哦,看那个。>> 你可以看到它说“Python 爱 VS 工具箱”。注意因为我在定义那条消息的 app.py 内部设置了这个断点,我实际上,为了命中断点,我需要向这个 API 发送一条消息。那么让我们做吧。你喜欢的或者可能想知道情感的短语是什么,我不知道。>> 让我们说“Champion”。>> “Champion”,让我们看看 champion 的情感是什么。

[24:43] 所以请注意,我这里有一个旋转的轮子。>> 因为我们正处于世界大赛期间,今天或明天就会产生冠军。>> 哦,哇。好吧,让我们看看情绪如何。所以你可以看到 VS Code,这里的图标在闪烁橙色,这意味着它做了些神奇的事情。它所做的就是命中了我们 API 内部的这个断点。让我们花点时间探索一下调试菜单。所以注意它收到了我们的消息“Champion”。

[25:15] 这是我们的局部变量窗口。我也可以有我的监视窗口,我有我的调用堆栈,所有这些好东西,我还可以逐行步进。所以要步过,我实际上可以使用那里的调试工具栏,或者我可以直接按“F10”,所以我就在这里使用[听不清]。你会注意到,当我步进时,它说了“okay”,记住我们说我们首先确定所有情绪都是积极的,然后我们通过这个 if 语句运行它们。所以我再步过一次。为了继续,我可以点击这里的箭头来继续并一直运行到底,或者我可以直接按“F5”。

[25:53] 所以我要再次点击“继续”。如果我导航回我的 API,我看到单词“Champion”是积极的。>> 不错。是的,很酷。>> 好的,让我们做另一个测试。>> 让我们做另一个测试。>> 是的。让我们测试一条新消息。>> 好的,让我们测试一条新消息。这次,我要重新运行这个,但我要尝试用不同的方式重新运行它,我仍然使用调试器,但方式略有不同。所以这次,要在第 15 行设置断点运行这个应用程序,我将使用我之前提到的调试图标。

[26:25] 这里真正酷的是,调试图标让我可以访问我之前看到的配置菜单。所以如果我点击这个“下拉菜单”并选择“添加配置”,它会做什么,然后我必须点击绿色的“播放”按钮,开始调试,它会把我带到同一个调试配置菜单。>> 好的。>> 你可能想知道,“哦 Tyrique,为什么我必须再次输入这个?”,这是因为我们上次的配置没有被保存,所以通过我们现在经历的这些步骤,我们正在保存那个配置,我马上就会向你证明。

[26:59] 所以我要再次导航到 Flask,放入那个路径,注意它创建了这个名为“launched.json”的文件。我们发现这个启动文件对于刚开始使用 VS code 的人来说非常可怕或令人生畏,但不要害怕,它只是一个数组。它是 JSON 格式,一个包含不同配置列表的数组。但注意那个。>> 一切都保存了。

[27:29] >> 它保存了,对吧,我的启动路径也保存了。我这里有一些不同的参数,它们是默认开启的,为了向你展示它保存在哪里,如果你使用这里的资源管理器按钮导航回你的文件资源管理器,那个 .VS code 文件夹,如果之前没有创建过,它会为你创建,你就会有那个逻辑配置保存下来。这很重要,因为,再次强调,你不必再次经历启动步骤来保存它。所以让我们回到我的调试器,你会注意到,看它说“这次”而不是像之前说的“AD 配置”或“无配置”,我得到了 Python flask。

[28:08] 关于调试,另一个重要的事情是要意识到,注意我目前正在“launch.json”文件内部。如果我在这里启动调试器,它实际上会抛出一个错误,因为它认为我在尝试调试这个 JSON。所以为了避免这种情况,我需要实际上回到我正在尝试调试的文件。所以我们要点击我们的绿色“播放”按钮。再次,我需要导航到。所以让我们尝试另一个词。

[28:39] >> 好的。所以这是一个情绪测试。我来自波士顿。>> 好的。>> 在波士顿,当我们喜欢某样东西时,我们会说“It's wicked cool”。>> “Wicked cool”。>> 现在“Wicked”有负面情绪,但“Cool”有正面情绪。所以让我们看看会发生什么。>> 让我们看看它的 Text bot 如何处理。所以“Wicked cool”。好的。再次注意,它闪烁橙色,意味着它命中了断点,并且通过这里的黄线验证了它命中了断点。同样非常酷的是,你在变量窗口中获取的所有信息,你实际上可以在这里悬停在变量上。

[29:16] >> 是的。>> 你得到了工具提示,它一开始不太配合,好了,它一开始很害羞,但来了。你可以看到所有不同的属性。再次,为了继续,我要按“F5”。我要导航回这个。>> 太好了。>> 它说“Wicked cool 是积极的”。>> 好的。是的。>> 这很酷。>> 非常好。>> 是的,非常棒。再次回顾一下,如果你从这里工具栏开始调试,你不会得到那个配置保存。

[29:48] 所以,实际上要把它保存下来,让你下次更高效,你需要使用调试图标,然后添加一个配置。>> 酷。>> 这真是非常非常酷的东西。那么让我们回顾一下我们讲过的一些内容,因为我知道我们已经讲了很多。使用虚拟环境,我们之前讲过,来帮助你管理不同应用程序的包。所以创建新环境,取决于你是 Mac 或 Linux 用户还是 Windows 用户,命令都在那里。>> 好的。>> 然后你可以使用我用的那个命令,我当时没有真正详细讲,但现在我要讲一下,就是 pip install -r requirements.txt,那是如果你的文件,或者如果你的项目有这样一个文件,你可以先创建一个虚拟环境,然后你可以安装其中列出的依赖项。

[30:36] 如果你愿意,你也可以安装单独的模块名称。>> 好的。>> 开始使用调试。当然,有多种方法可以做到这一点,但我推荐的方式,也是我喜欢的方式,实际上是使用虫子图标,它会带我进入调试配置菜单,我可以点击那个“下拉菜单”,点击“添加配置”,我可以选择我想要调试的配置。>> 第二次你必须重新输入所有操作,因为你没有保存它,你会直接到这里。>> 对,没错。把那个 Web 应用程序的路径放进去,然后那个“launch.json”就被创建了,然后记住,你不想调试“launch.json”文件,你实际上想回到你感兴趣的 Python 文件,然后点击绿色的运行按钮。

[31:17] >> 好的。>> 相当酷的东西,这就是我今天要给大家讲的全部内容了。>> 好的。>> 就是谈谈那个入门体验。>> 酷。所以想了解更多信息,人们可以访问 aka.ms/VST/pythonvscode。>> 是的。>> 那里有一个入门教程。关于 Visual Studio Code 中 Python 的更多信息。人们去哪里学习 Python 基础知识比较好呢?>> 网上有很多文档,我们的入门教程会教你,并链接到其他有用的东西、其他材料,但是的,例如,如果你想学习 Flask,他们提供的 Flask 文档非常出色。

[32:00] >> 好的。>> 不过,如果你对入门有任何问题,并且想直接与团队互动,我们的 Twitter 账号是 @PythonVSCode。>> 好的。>> 连在一起写,我们一直在与用户互动,试图了解哪些体验最好,哪些不好。>> 太棒了。>> 是的。太棒了。谢谢你邀请我。>> 谢谢你来参加。对于那些还没有怎么玩过 Visual Studio Code 的人来说,这是很好的介绍,同时也介绍了如何开始使用 Python,这很棒。>> 完美。是的。

[32:31] >> 谢谢。>> 太棒了。谢谢。>> 希望你们喜欢,我们下次在 Visual Studio Toolbox 再见。[音乐]

查看英文原始字幕

[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工作台中打开

搜索转录文本,选择行,复制带时间戳的引文,翻译。

在工作台中打开 →

署名

Microsoft Visual Studio 的“Getting Started with Python in Visual Studio Code”(https://www.youtube.com/@visualstudio),根据 CC BY 3.0 许可(https://creativecommons.org/licenses/by/3.0/)。源视频:https://www.youtube.com/watch?v=7EXd4_ttIuw。本页是视频的文本转录,添加了段落分隔和时间戳;创作者与 Vidleaf 无关联,也不为其背书。Vidleaf 添加了 AI 标点和 AI 生成的摘要;原始措辞仍可在本页查看。

您是创作者或权利持有人吗?请求更正或移除:copyright@vidleaf.app(参见关于这些页面)。

最后更新于