Configure your local environment for deploying Python web apps on Azure
This article walks you through setting up your local environment to develop Python web apps and deploy them to Azure. Your web app can be pure Python or use one of the common Python-based web frameworks like Django, Flask, or FastAPI.
Python web apps developed locally can be deployed to services such as Azure App Service, Azure Container Apps, or Azure 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 the Set 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:
- Quickstart: Create a Python (Django or Flask) web app in Azure App Service.
- Tutorial: Deploy a Python (Django or Flask) web app with PostgreSQL in Azure
- Tutorial: Deploy a Python web app to Azure with managed identity
Working with Visual Studio Code
The Visual Studio Code integrated development environment (IDE) is an easy way to develop Python web apps and work with Azure resources that web apps use.
In VS code, you work with Azure resources through VS Code extensions. You can install extensions from the Extensions 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:
The Azure 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.
The Azure 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:
Extensions in Visual Studio Code are accessible as you would expect in a typical IDE interface and with rich keyword support using the VS 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.
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 the PyCharm IDE. The web app can be deployed to an Azure App Service using the
az 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.
Azure CLI commands
When working locally with web apps using the Azure CLI commands, you'll typically work with the following commands:
|az webapp||Manages web apps. Includes the subcommands create to create a web app and the up to create and deploy from a local workspace|
|az container app||Manages Azure Container Apps.|
|az staticwebapp||Manages Azure Static Web Apps.|
|az group||Manages resource groups and template deployments. Use the subcommand create to a resource group to put your Azure resources in.|
|az appservice||Manages App Service plans.|
|az config||Managed 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 using az webapp up. Run the command in the root directory of your web app.
For more about this example, see Quickstart: 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 an Azure 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 the Azure SDK for Python. This SDK is discussed extensively in the section Use 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-practice 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 with
pip. Or put the Python package index (Pypi) name in your requirements.txt file.
|SDK docs||Install||Python package index|
|Azure Storage Blobs||
|Azure Cosmos DB||
|Azure Key Vault Secrets||
The azure-identity package allows your web app to authenticate with Azure Active Directory (Azure AD). For authentication in your web app code, it's recommended that you use the DefaultAzureCredential in the
azure-identity package. Here's an example of how to access Azure Storage. The pattern is similar for other Azure resources.
from azure.identity import DefaultAzureCredential from azure.storage.blob import BlobServiceClient azure_credential = DefaultAzureCredential() blob_service_client = BlobServiceClient( account_url=account_url, credential=azure_credential)
DefaultAzureCredential will look in predefined locations for account information, for example, in environment variables, in the VS Code Account extension, or from the Azure CLI sign-in. For in-depth information on the
DefaultAzureCredential logic, see Authenticate 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 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.
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 a virtual environment.
Step 1: Download the frameworks with pip.
Step 2: Create a hello world app.
Create a sample project using the django-admin startproject command. The project includes a manage.py file that is the entry point for running the app.
django-admin startproject hello_world
Step 3: Run the code locally.
Step 4: Browse the hello world app.
At this point, add a requirements.txt file and then you can deploy the web app to Azure or containerize it with Docker and then deploy it.
Submit and view feedback for