Quickstart: Create a Python app in Azure App Service on Linux

In this article, you deploy a simple Python app to App Service on Linux, which provides a highly scalable, self-patching web hosting service. You use the Azure command-line interface (the Azure CLI) through the interactive, browser-based Azure Cloud Shell, so you can follow the steps use a Mac, Linux, or Windows computer.

Run a sample Python app in App Service in Azure

Prerequisites

To complete this quickstart:

Download the sample locally

In a terminal window, run the following commands to clone the sample application to your local machine, and go to the directory with the sample code.

git clone https://github.com/Azure-Samples/python-docs-hello-world
cd python-docs-hello-world

The repository contains an application.py, which tells App Service that the repository contains a Flask app. For more information, see Container startup process and customizations.

Run the app locally

Run the application locally so that you see how it should look when you deploy it to Azure. Open a terminal window and use the commands below to install the required dependencies and launch the built-in development server.

# In Bash (for Linux or Mac)
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
FLASK_APP=application.py flask run
# In Powershell (for Windows)
py -3 -m venv env
env\scripts\activate
pip install -r requirements.txt
Set-Item Env:FLASK_APP ".\application.py"
flask run

Open a web browser, and go to the sample app at http://localhost:5000/.

You see the Hello World! message from the sample app displayed in the page.

Run a sample Python app locally

In your terminal window, press Ctrl+C to exit the web server.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell lets you use either bash or PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

To launch Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Example of Try It for Azure Cloud Shell
Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. Launch Cloud Shell in a new window
Select the Cloud Shell button on the top-right menu bar in the Azure portal. Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Launch Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.

  4. Press Enter to run the code.

Download the sample

In the Cloud Shell, create a quickstart directory and then change to it.

mkdir quickstart

cd $HOME/quickstart

Next, run the following command to clone the sample app repository to your quickstart directory.

git clone https://github.com/Azure-Samples/python-docs-hello-world

While running, it displays information similar to the following example:

Cloning into 'python-docs-hello-world'...
remote: Enumerating objects: 43, done.
remote: Total 43 (delta 0), reused 0 (delta 0), pack-reused 43
Unpacking objects: 100% (43/43), done.
Checking connectivity... done.

Create a web app

Change to the directory that contains the sample code and run the az webapp up command.

In the following example, replace <app-name> with a globally unique app name (valid characters are a-z, 0-9, and -).

cd python-docs-hello-world

az webapp up -n <app-name>

This command may take a few minutes to run. While running, it displays information similar to the following example:

The behavior of this command has been altered by the following extension: webapp
Creating Resource group 'appsvc_rg_Linux_CentralUS' ...
Resource group creation complete
Creating App service plan 'appsvc_asp_Linux_CentralUS' ...
App service plan creation complete
Creating app '<app-name>' ....
Webapp creation complete
Creating zip with contents of dir /home/username/quickstart/python-docs-hello-world ...
Preparing to deploy contents to app.
All done.
{
  "app_url": "https:/<app-name>.azurewebsites.net",
  "location": "Central US",
  "name": "<app-name>",
  "os": "Linux",
  "resourcegroup": "appsvc_rg_Linux_CentralUS ",
  "serverfarm": "appsvc_asp_Linux_CentralUS",
  "sku": "BASIC",
  "src_path": "/home/username/quickstart/python-docs-hello-world ",
  "version_detected": "-",
  "version_to_create": "python|3.7"
}

Note

The az webapp up command does the following actions:

Browse to the app

Browse to the deployed application using your web browser.

http://<app-name>.azurewebsites.net

The Python sample code is running in App Service on Linux with a built-in image.

Run a sample Python app in Azure

Congratulations! You've deployed your first Python app to App Service on Linux.

Update locally and redeploy the code

In the Cloud Shell, enter code application.py to open the Cloud Shell editor.

Open application.py in the Cloud Shell editor

Make a small change to the text in the call to return:

return "Hello Azure!"

Save your changes and exit the editor. Use the command ^S to save and ^Q to exit.

Redeploy the app using the az webapp up command. Substitute the name of your app for <app-name>, and specify a location for <location-name> (using one of the values shown from the az account list-locations command).

az webapp up -n <app-name> -l <location-name>

Once deployment has completed, switch back to the browser window that opened in the Browse to the app step, and refresh the page.

Run an updated sample Python app in Azure

Manage your new Azure app

Go to the Azure portal to manage the app you created.

From the left menu, select App Services, and then select the name of your Azure app.

Navigate to your Python app in App Services in the Azure portal

You see your app's Overview page. Here, you can perform basic management tasks like browse, stop, start, restart, and delete.

Manage your Python app in the Overview page in the Azure portal

The left menu provides different pages for configuring your app.

Clean up resources

In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:

az group delete --name myResourceGroup

This command may take a minute to run.

Next steps