Create your first Python function in Azure (preview)


Python for Azure Functions is currently in preview. To receive important updates, subscribe to the Azure App Service announcements repository on GitHub.

This quickstart article walks you through how to use the Azure CLI to create your first serverless Python function app running on Linux. The function code is created locally and then deployed to Azure by using the Azure Functions Core Tools. To learn more about preview considerations for running your function apps on Linux, see this Functions on Linux article.

The following steps are supported on a Mac, Windows, or Linux computer.


To build and test locally, you will need to:

To publish and run in Azure:

Create and activate a virtual environment

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named .env.

# In Bash
python3.6 -m venv .env
source .env/bin/activate

# In PowerShell
py -3.6 -m venv .env

Create a local Functions project

You can now create a local Functions project. This directory is the equivalent of a Function App in Azure. It can contain multiple functions that share the same local and hosting configuration.

In the terminal window or from a command prompt, run the following command:

func init MyFunctionProj

Pick python as the desired runtime.

Select a worker runtime:
1. dotnet
2. node
3. python

You see something like the following output.

Installing wheel package
Installing azure-functions package
Installing azure-functions-worker package
Running pip freeze
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing /MyFunctionProj/.vscode/extensions.json

A new folder named MyFunctionProj is created. To continue, change directory to this folder.

cd MyFunctionProj

Create a function

To create a function, run the following command:

func new

Choose HTTP Trigger as the template and provide a function name of HttpTrigger.

Select a template:
1. Blob trigger
2. Cosmos DB trigger
3. Event Grid trigger
4. Event Hub trigger
5. HTTP trigger
6. Queue trigger
7. Service Bus Queue trigger
8. Service Bus Topic trigger
9. Timer trigger

Choose option: 5
Function name: HttpTrigger

You see something like the following output.

Writing /MyFunctionProj/HttpTrigger/sample.dat
Writing /MyFunctionProj/HttpTrigger/
Writing /MyFunctionProj/HttpTrigger/function.json
The function "HttpTrigger" was created successfully from the "HTTP trigger" template.

A sub-folder named HttpTrigger is created. This contains which is the primary script file and function.json file which describes the trigger and bindings used by the function. To learn more about the programming model, you can refer to the Azure Functions Python developer guide.

Run the function locally

Use the following command to run the Functions host locally.

func host start

When the Functions host starts, it outputs the URL of your HTTP-triggered function. (Note that the entire output has been truncated for readability.)

            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
Now listening on:
Application started. Press Ctrl+C to shut down.

Http Functions:

        HttpTrigger: http://localhost:7071/api/HttpTrigger

Copy the URL of your function from the output and paste it into your browser's address bar. Append the query string ?name=<yourname> to this URL and execute the request.


The following screenshot shows the response from the function, when it is triggered from the browser:


You are now ready to create a Function App and other required resources for publishing to Azure.

Create a resource group

Create a resource group with the az group create. An Azure resource group is a logical container into which Azure resources like function apps, databases, and storage accounts are deployed and managed.

The following example creates a resource group named myResourceGroup.
If you are not using Cloud Shell, sign in first using az login.

az group create --name myResourceGroup --location westeurope

You generally create your resource group and the resources in a region near you. To see all supported locations for App Service plans, run the az appservice list-locations command.

Create an Azure Storage account

Functions uses a general-purpose account in Azure Storage to maintain state and other information about your functions. Create a general-purpose storage account in the resource group you created by using the az storage account create command.

In the following command, substitute a globally unique storage account name where you see the <storage_name> placeholder. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.

az storage account create --name <storage_name> --location westeurope --resource-group myResourceGroup --sku Standard_LRS

After the storage account has been created, the Azure CLI shows information similar to the following example:

  "creationTime": "2017-04-15T17:14:39.320307+00:00",
  "id": "/subscriptions/bbbef702-e769-477b-9f16-bc4d3aa97387/resourceGroups/myresourcegroup/...",
  "kind": "Storage",
  "location": "westeurope",
  "name": "myfunctionappstorage",
  "primaryEndpoints": {
    "blob": "",
    "file": "",
    "queue": "",
    "table": ""
    // Remaining output has been truncated for readability.

Create a Linux function app in Azure

The function app provides an environment for executing your function code. It lets you group functions as a logical unit for easier management, deployment, and sharing of resources. Create a Python function app running on Linux using the az functionapp create command.

Run the following command using a unique function app name in place of the <app_name> placeholder and the storage account name for <storage_name>. The <app_name> is also the default DNS domain for the function app. This name needs to be unique across all apps in Azure.

az functionapp create --resource-group myResourceGroup --os-type Linux \
--consumption-plan-location westeurope  --runtime python \
--name <app_name> --storage-account  <storage_name>


If you have an existing resource group named myResourceGroup with any non-Linux App Service apps, you must use a different resource group. You can't host both Windows and Linux apps in the same resource group.

After the function app has been created, you will see the following message:

Your serverless Linux function app 'myfunctionapp' has been successfully created.
To active this function app, publish your app content using Azure Functions Core Tools or the Azure portal.

You are now ready to publish your local functions project to the Function App in Azure.

Deploy the function app project to Azure

Using the Azure Functions Core Tools, run the following command. Replace <app_name> with the name of your app from the previous step.

func azure functionapp publish <app_name>

You will see something like the following output, which has been truncated for readability.

Getting site publishing info...


Preparing archive...
Uploading content...
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...

Test the function

Use cURL to test the deployed function on a Mac or Linux computer or using Powershell on Windows. Execute the following cURL command, replacing the <app_name> placeholder with the name of your function app. Append the query string &name=<yourname> to the URL.

Invoke-WebRequest -Uri "https://<app_name><yourname>"
curl https://<app_name><yourname>

Function response shown in a browser.

If you don't have cURLor Invoke-WebRequest available in your command line, enter the same URL in the address of your web browser. Again, replace the <app_name> placeholder with the name of your function app, and append the query string &name=<yourname> to the URL and execute the request.


Function response shown in a browser.

Clean up resources

Other quickstarts in this collection build upon this quickstart. If you plan to continue on to work with subsequent quickstarts or with the tutorials, do not clean up the resources created in this quickstart. If you do not plan to continue, use the following command to delete all resources created by this quickstart:

az group delete --name myResourceGroup

Type y when prompted.

Next steps

Learn more about developing Azure Functions using Python.