Quickstart: Create a Python app in Azure App Service on Linux
In this quickstart, you deploy a Python web app to App Service on Linux, Azure's highly scalable, self-patching web hosting service. You use the local Azure command-line interface (CLI) on a Mac, Linux, or Windows computer. The web app you configure uses a free App Service tier, so you incur no costs in the course of this article.
If you prefer to deploy apps through an IDE, see Deploy Python apps to App Service from Visual Studio Code.
Set up your initial environment
Before you begin, you must have the following:
- Have an Azure account with an active subscription. Create an account for free.
- Install Python 3.6 or higher.
- Install the Azure CLI 2.0.80 or higher, with which you run commands in any shell to provision and configure Azure resources.
Open a terminal window and check your Python version is 3.6 or higher:
Check that your Azure CLI version is 2.0.80 or higher:
Then sign in to Azure through the CLI:
This command opens a browser to gather your credentials. When the command finishes, it shows JSON output containing information about your subscriptions.
Once signed in, you can run Azure commands with the Azure CLI to work with resources in your subscription.
Clone the sample
Clone the sample repository with the following command, then navigate into the folder. (Install git if you don't have git already.)
git clone https://github.com/Azure-Samples/python-docs-hello-world cd python-docs-hello-world
The sample code contains an application.py file, which tells App Service that the code contains a Flask app. For more information, see Container startup process.
Run the sample
First create a virtual environment and install dependencies:
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt
Then set the
FLASK_APP environment variable to the app's entry module and run the Flask development server:
export FLASK_APP=application.py flask run
Open a web browser, and go to the sample app at
http://localhost:5000/. The app displays the message Hello World!.
In your terminal window, press Ctrl+C to exit the Flask development server.
Deploy the sample
Deploy the code in your local folder (python-docs-hello-world) using the
az webapp up command:
az webapp up --sku F1 -n <app-name>
- If the
azcommand is not recognized, be sure you have the Azure CLI installed as described in Set up your initial environment.
<app_name>with a name that's unique across all of Azure (valid characters are
-). A good pattern is to use a combination of your company name and an app identifier.
--sku F1argument creates the web app on the Free pricing tier. Omit this argument to use a faster premium tier, which incurs an hourly cost.
- You can optionally include the argument
<location_name>is an Azure region such as centralus, eastasia, westeurope, koreasouth, brazilsouth, centralindia, and so on. You can retrieve a list of allowable regions for your Azure account by running the
az account list-locationscommand.
- If you see the error, "Could not auto-detect the runtime stack of your app," make sure you're running the command in the python-docs-hello-world folder that contains the requirements.txt file. (See Troubleshooting auto-detect issues with az webapp up (GitHub).)
The command may take a few minutes to complete. While running, it provides messages about creating the resource group, the App Service plan and hosting app, configuring logging, then performing ZIP deployment. It then gives the message, "You can launch the app at http://<app-name>.azurewebsites.net", which is the app's URL on Azure.
az webapp up command does the following actions:
Browse to the app
Browse to the deployed application in your web browser at the URL
The Python sample code is running a Linux container in App Service using a built-in image.
Congratulations! You've deployed your Python app to App Service.
In your favorite code editor, open application.py and update the
hello function as follows. This change adds a
def hello(): print("Handling request to home page.") return "Hello Azure!"
Save your changes and exit the editor.
Redeploy the app using the
az webapp up command again:
az webapp up
This command uses values that are cached locally in the .azure/config file, including the app name, resource group, and App Service plan.
Once deployment is complete, switch back to the browser window open to
http://<app-name>.azurewebsites.net. Refresh the page, which should display the modified message:
Visual Studio Code provides powerful extensions for Python and Azure App Service, which simplify the process of deploying Python web apps to App Service. For more information, see Deploy Python apps to App Service from Visual Studio Code.
You can access the console logs generated from inside the app and the container in which it runs. Logs include any output generated using
To stream logs, run the following command:
az webapp log tail
Refresh the app in the browser to generate console logs, which include messages describing HTTP requests to the app. If no output appears immediately, try again in 30 seconds.
You can also inspect the log files from the browser at
To stop log streaming at any time, type Ctrl+C.
Manage the Azure app
Go to the Azure portal to manage the app you created. Search for and select App Services.
Select the name of your Azure app.
Selecting the app opens its Overview page, where you can perform basic management tasks like browse, stop, start, restart, and delete.
The App Service menu provides different pages for configuring your app.
Clean up resources
In the preceding steps, you created Azure resources in a resource group. The resource group has a name like "appsvc_rg_Linux_CentralUS" depending on your location. If you use an App Service SKU other than the free F1 tier, these resources incur ongoing costs (see App Service pricing).
If you don't expect to need these resources in the future, delete the resource group by running the following command:
az group delete
The command uses the resource group name cached in the .azure/config file.
The command may take a minute to complete.