教程:将 Python 应用从 Visual Studio Code 部署到 Linux 上的 Azure 应用服务Tutorial: Deploy Python apps to Azure App Service on Linux from Visual Studio Code

本文详细介绍如何使用 Azure 应用服务扩展通过 Visual Studio Code 将 Python 应用程序部署到 Linux 上的 Azure 应用服务。This article walks you through using Visual Studio Code to deploy a Python application to Azure App Service on Linux using the Azure App Service extension.

如果你在执行本教程中的任何步骤时遇到问题,请告知我们详情。If you encounter issues with any of the steps in this tutorial, we'd love to hear the details. 请使用每篇文章末尾的 遇到问题?请告诉我们。Use the Having issues? Let us know. 链接来提交反馈。link at the end of each article to submit feedback.

有关演示视频,请观看虚拟 PyCon 2020 中的使用 VS Code 和 Azure 应用服务生成 WebApps (youtube.com)。For a demonstration video, see Build WebApps with VS Code and Azure App Service (youtube.com) from virtual PyCon 2020.

备注

如果希望通过 CLI 部署应用,请参阅快速入门:在 Linux 上的 Azure 应用服务中创建 Python 应用If you prefer to deploy apps through the CLI, see Quickstart: Create a Python app in Azure App Service on Linux.

提示

Linux 上的 Azure 应用服务在预定义的 Docker 容器中运行源代码。Azure App Service on Linux runs your source code in a pre-defined Docker container. 该容器使用 Gunicorn Web 服务器来运行具有 Python 3.6 及更高版本的应用。That container runs apps with Python 3.6+ using the Gunicorn web server. 此容器的特征详见为 Linux 上的应用服务配置 Python 应用一文。The characteristics of this container are described on Configure Python apps for App Service on Linux. 容器定义位于 github.com/Azure-App-Service/pythonThe container definitions are on github.com/Azure-App-Service/python.

配置环境Configure your environment

登录 AzureSign in to Azure

安装 Azure 扩展以后,请登录到 Azure 帐户:Once you've installed the Azure extension, sign into your Azure account:

  1. 导航到 Azure 资源管理器Navigate to the Azure explorer

  2. 选择“登录到 Azure”并按照提示操作。Select Sign in to Azure and follow the prompts. (如果已安装多个 Azure 扩展,请针对你使用的区域选择一个扩展,如应用服务、Functions 等。)(If you have multiple Azure extensions installed, select the one for the area in which you're working, such as App Service, Functions, etc.)

    通过 VS Code 登录到 Azure

  3. 登录后,验证状态栏中显示了“Azure:已登录”,且 Azure 资源管理器中显示了你的订阅:After signing in, verify that Azure: Signed In" appears in the Status Bar and your subscription(s) appears in the Azure explorer:

    Visual Studio Code 状态栏,其中显示了 Azure 帐户

    Visual Studio Code 的 Azure 应用服务资源管理器,其中显示了订阅

备注

如果出现错误“找不到名为 [订阅 ID] 的订阅”,这可能是因为你使用了代理,因此无法访问 Azure API。If you see the error "Cannot find subscription with name [subscription ID]", this may be because you are behind a proxy and unable to reach the Azure API. 在终端中使用代理信息配置 HTTP_PROXYHTTPS_PROXY 环境变量:Configure HTTP_PROXY and HTTPS_PROXY environment variables with your proxy information in your terminal:

# Windows
set HTTPS_PROXY=https://username:password@proxy:8080
set HTTP_PROXY=http://username:password@proxy:8080
# macOS/Linux
export HTTPS_PROXY=https://username:password@proxy:8080
export HTTP_PROXY=http://username:password@proxy:8080

存在问题?请告诉我们。Having issues? Let us know.