Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Configure your local environment for deploying Python web apps on Azure

  • Article
  • 2025-02-04
Feedback

In this article

This article walks you through setting up your local environment to develop Pythonweb apps and deploy them to Azure. Your web app can be pure Python or use one of the common Python-based web frameworks likeDjango,Flask, orFastAPI.

Python web apps developed locally can be deployed to services such asAzure App Service,Azure Container Apps, orAzure Static Web Apps. There are many options for deployment. For example, for App Service deployment, you can choose to deploy from code, a Docker container, or a Static Web App. If you deploy from code, you can deploy with Visual Studio Code, with the Azure CLI, from a local Git repository, or with GitHub actions. If you deploy in a Docker Container, you can do so from Azure Container Registry, Docker Hub, or any private registry.

Before continuing with this article, we suggest you review theSet up your dev environment for guidance on setting up your dev environment for Python and Azure. Below, we'll discuss setup and configuration specific to Python web app development.

After you get your local environment setup for Python web app development, you'll be ready to tackle these articles:

Working with Visual Studio Code

TheVisual Studio Code integrated development environment (IDE) is an easy way to develop Python web apps and work with Azure resources that web apps use.

Tip

Make sure you have thePython extension installed. For an overview of working with Python in VS Code, seeGetting Started with Python in VS Code.

In VS Code, you work with Azure resources throughVS Code extensions. You can install extensions from theExtensions View or the key combination Ctrl+Shift+X. For Python web apps, you'll likely be working with one or more of the following extensions:

  • TheAzure App Service extension enables you to interact with Azure App Service from within Visual Studio Code. App Service provides fully managed hosting for web applications including websites and web APIs.

  • TheAzure Static Web Apps extension enables you to create Azure Static Web Apps directly from VS Code. Static Web Apps is serverless and a good choice for static content hosting.

  • If you plan on working with containers, then install:

    • TheDocker extension to build and work with containers locally. For example, you can run a containerized Python web app on Azure App Service usingWeb Apps for Containers.

    • TheAzure Container Apps extension to create and deploy containerized apps directly from Visual Studio Code.

  • There are other extensions such as theAzure Storage,Azure Databases, andAzure Resources extensions. You can always add these and other extensions as needed.

Extensions in Visual Studio Code are accessible as you would expect in a typical IDE interface and with rich keyword support using theVS Code command palette. To access the command palette, use the key combination Ctrl+Shift+P. The command palette is a good way to see all the possible actions you can take on an Azure resource. The screenshot below shows some of the actions for App Service.

A screenshot of the Visual Studio Code command palette for App Service.

Working with Dev Containers in Visual Studio Code

Python developers often rely on virtual environments to create an isolated and self-contained environment for a specific project. Virtual environments allow developers to manage dependencies, packages, and Python versions separately for each project, avoiding conflicts between different projects that might require different package versions.

While there are popular options available in Python for managing environments likevirtualenv orvenv, theVisual Studio Code Dev Containerextension (based on theopen Dev Container specification) lets you use aDocker container as a full-featured containerized environment. It enables developers to define a consistent and easily reproducible toolchain with all the necessary tools, dependencies, and extensions pre-configured. This means if you have system requirements, shell configurations, or use other languages entirely, you can use a Dev Container to explicitly configure all of those parts of your project that might live outside of a basic Python environment.

For example, a developer can configure a single Dev Container to include everything needed to work on a project, including a PostgreSQL database server along with the project database and sample data, a Redis server, Nginx, front-end code, client libraries like React, and so on. In addition, the container would contain the project code, the Python runtime, and all the Python project dependencies with the correct versions. Finally, the container can specify Visual Studio Code extensions to be installed so the entire team has the same tooling available. So when a new developer joins the team, the whole environment, including tooling, dependencies, and data, is ready to be cloned to their local machine, and they can begin working immediately.

SeeDeveloping inside a Container.

Working with Visual Studio 2022

Visual Studio 2022 is a full-featured integrated development environment (IDE) with support for Python application development and many built-in tools and extensions to access and deploy to Azure resources. While most documentation for building Python web apps on Azure focuseson using Visual Studio Code, Visual Studio 2022 is a great option if you alreadyhave it installed, you're comfortable with using it, and are using it for .NET orC++ projects.

  • In general, seeVisual Studio | Python documentation for all documentation related to using Python on Visual Studio 2022.

  • For setup steps, seeInstall Python support in Visual Studio which walks you through the steps of installing the Python workload into Visual Studio 2022.

  • For general workflow of using Python for web development, seeQuickstart: Create your first Python web app using Visual Studio. This article is useful for understanding how to build a Python web applicationfrom scratch (but does not include deployment to Azure).

  • For using Visual Studio 2022 to manage Azure resources and deploy to Azure,seeAzure Development with Visual Studio. While much of the documentation here specifically mentions.NET, the tooling for managing Azure resources and deploying to Azure works thesame regardless of the programming language.

  • When there's no built-in tool available in Visual Studio 2022 for a givenAzure management or deployment task, you can always useAzure CLI commands.

Working with other IDEs

If you're working in another IDE that doesn't have explicit support for Azure, then you can use the Azure CLI to manage Azure resources. In the screenshot below, a simple Flask web app is open in thePyCharm IDE. The web app can be deployed to an Azure App Service using theaz webapp up command. In the screenshot, the CLI command runs within the PyCharm embedded terminal emulator. If your IDE doesn't have an embedded emulator, your can use any terminal and the same command. The Azure CLI must be installed on your computer and be accessible in either case.

A screenshot of the PyCharm IDE with an Azure CLI command deploying a web app.

Azure CLI commands

When working locally with web apps using theAzure CLI commands, you'll typically work with the following commands:

CommandDescription
az webappManages web apps. Includes the subcommandscreate andup to create a web app or to create and deploy from a local workspace, respectively.
az container appManages Azure Container Apps.
az staticwebappManages Azure Static Web Apps.
az groupManages resource groups and template deployments. Use the subcommandcreate to make a resource group to put your Azure resources in.
az appserviceManages App Service plans.
az configManages Azure CLI configuration. To save keystrokes, you can define a default location or resource group that other commands use automatically.

Here's an example Azure CLI command to create a web app and associated resources, and deploy it to Azure in one command usingaz webapp up. Run the command in the root directory of your web app.

az webapp up \    --runtime PYTHON:3.9 \    --sku B1 \    --logs

For more about this example, seeQuickstart: Deploy a Python (Django or Flask) web app to Azure App Service.

Keep in mind that for some of your Azure workflow you can also use the Azure CLI from anAzure Cloud Shell. Azure Cloud Shell is an interactive, authenticated, browser-accessible shell for managing Azure resources.

Azure SDK key packages

In your Python web apps, you can refer programmatically to Azure services using theAzure SDK for Python. This SDK is discussed extensively in the sectionUse the Azure libraries (SDK) for Python. In this section, we'll briefly mention some key packages of the SDK that you'll use in web development. And, we'll show an example around the best practices for authenticating your code with Azure resources.

Below are some of the packages commonly used in web app development. You can install packages in your virtual environment directly withpip. Or put the Python package index (Pypi) name in yourrequirements.txt file.

SDK docsInstallPython package index
Azure Identitypip install azure-identityazure-identity
Azure Storage Blobspip install azure-storage-blobazure-storage-blob
Azure Cosmos DBpip install azure-cosmosazure-cosmos
Azure Key Vault Secretspip install azure-keyvault-secretsazure-keyvault-secrets

Theazure-identity package allows your web app to authenticate with Microsoft Entra ID. For authentication in your web app code, it's recommended that you use theDefaultAzureCredential in theazure-identity package. Here's an example of how to access Azure Storage. The pattern is similar for other Azure resources.

from azure.identity import DefaultAzureCredentialfrom azure.storage.blob import BlobServiceClientazure_credential = DefaultAzureCredential()blob_service_client = BlobServiceClient(    account_url=account_url,    credential=azure_credential)

TheDefaultAzureCredential will look in predefined locations for account information, for example, in environment variables or from the Azure CLI sign-in. For in-depth information on theDefaultAzureCredential logic, seeAuthenticate Python apps to Azure services by using the Azure SDK for Python.

Python-based web frameworks

In Python web app development, you often work with Python-based web frameworks. These frameworks provide functionality, such as page templates, session management, database access, and easy access to HTTP request and response objects. Frameworks enable you to avoid the need for you to have to reinvent the wheel for common functionality.

Three common Python web frameworks areDjango,Flask, orFastAPI. These and other web frameworks can be used with Azure.

Below is an example of how you might get started quickly with these frameworks locally. Running these commands, you'll end up with an application, albeit a simple one that could be deployed to Azure. Run these commands inside avirtual environment.

Step 1: Download the frameworks withpip.

pip install Django

Step 2: Create a hello world app.

Create a sample project using thedjango-admin startproject command. The project includes amanage.py file that is the entry point for running the app.

django-admin startproject hello_world

Step 3: Run the code locally.

Django uses WSGI to run the app.

python hello_world\manage.py runserver

Step 4: Browse the hello world app.

http://127.0.0.1:8000/

At this point, add arequirements.txt file and then you can deploy the web app to Azure or containerize it with Docker and then deploy it.

Next steps


Feedback

Was this page helpful?

YesNo

In this article