Azure Function Python Example

How To Create Azure Functions In Python using Azure Portal

In this Azure tutorial, we will discuss Azure functions Python Example including the steps to Deploy Python Azure Function To Azure From Visual Studio Code and Create Python Azure Function In Azure Portal.

Subscribe to Our YouTube Channel for more videos

Azure Function Python Example

Well, here we will discuss An excellent Azure functions Python example.

Azure Functions In Python

Well, like we are creating the Azure Function using C#, JavaScript, etc. In the same way, you can also create an Azure Function using Python.

Prerequisites

Before starting the development, we should know what is needed to create Azure function in Python.

  • Make sure to install the Visual Studio Code along with Azure Functions Extensions if you have not yet installed the Visual Studio Code. Install the Visual Studio Code now.
  • The next thing you need is Python 3.7.x or 3.6.x or 3.8.x etc
  • An Azure Free Account or Azure Subscription.

Now, Assuming we are ready with all the Prerequisites, Let’s start the actual development of the Python Azure Function.

1. Open the Visual Studio Code, click on the Azure Button from the left side, and then click on the Create New Project button in the Functions Explorer as highlighted below.

azure function python

2. Now is the time to choose the location to save your Azure Function project. You can click on the Browse button to navigate to the local path where you want to save your Azure Function project.

How To Create Azure Functions In Python

3. This step is very important. Select the language Python to create the Azure Function Project.

Create Azure Functions In Python

4. You need to select the Python interpreter. Make sure to select the correct version. Specify the location of your Python 3.6.x executable. Again, make sure you’re using a compatible version of Python.

python azure function

5. The next step is to choose the trigger that will invoke your Azure Function, and it is very important to select the required trigger based on your requirements. Let’s select the Basic and simplest trigger option, the HTTP trigger.

azure functions python tutorial

6. Now, the next step is you need to provide a unique name for your Azure Function, like the one below

azure functions tutorial python

7. The next step is to choose the Authorization level for your Azure Function. Choose the Authorization level as Function here. You can choose any of the options based on your requirements.

azure function app python

8. Select the Open in the new window, or you can choose the Open in the current window based on your choice.

azure function in python

If you are trying to create the Azure Function in Python for the first time then it might ask you to install the below-recommended extensions, you can click on the install button as highlighted below.

create azure function python

Suppose you have not installed Python by now. In that case, It might prompt you to install Python, Click on the Open button as highlighted below, which will redirect to Python’s official website to download Python 3.6 or the latest version for your operating system.

azure functions python example

You can download the Python 3.6.x version for the operating system that you are using and install the same on your machine.

Now you can see the Azure Function Project is already created using Python and is ready to use. You can see the _init._.py class file was created with the below piece of code

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )

Now, it might show you the Below Pop saying, “Linter pylint is not installed.” So click on the Install button to install the Linter pylint.

How To build Azure Functions In Python

Test Python Azure Function Locally

Now, we will run and check if the Azure Function is working fine or not. Press F5 to run the Azure Function or click on the Run button from the Visual Studio Code.

You can see below we got the Azure Python Function URL

The URL looks like below

http://localhost:7071/api/MyNewPythonfunction

How To make Azure Functions In Python

Now open your Favorite Browser and Paste the Function URL, Now you can able to see below, it is asking us to Provide the name value parameter in the query string.

azure python function

Now, I am appending the ?name=raj with the Azure Function URL. You can see we got the expected output below

import azure.functions could not be resolved

This is the way to create the Python Azure function in Visual Studio code. So, our Python Azure Function is working as expected without any issues. Now it’s time to deploy your Python Azure Function to Azure.

Deploy Python Azure Function To Azure From Visual Studio Code

Follow the below steps to deploy the Python Azure Function to Azure

Now click on the Azure button again and then click on the Deploy to Function App button, as highlighted below.

azure functions with python

Now click on the + Create new Function App in Azure button.

azure function python tutorial

The next step is to provide a unique name for the function app and click on Enter.

azure functions in python

Select the runtime stack version from the below window

import "azure.functions" could not be resolved

The next is, you need to select the region for the new resource and Now it will start deploying the Azure Python Function to the Azure Portal.

python azure function example

This way will help you write Azure Functions In Python and Deploy your Azure Function In Python.

How To Create Python Azure Function In Azure Portal

Follow the below steps.

  1. Log in to the Azure Portal (https://portal.azure.com/)
  2. Search for the Function App in the Azure Portal and click on the search result
azure function with python

3. On the Azure Function App page, click on the + Add button to create a new Azure Function App.

The next step is, On the Create Function App Page, Provide the below Details

  • Subscription: Choose the Correct Azure Subscription that you want to use here.
  • Resource Group: You can choose your existing Resource Group, or if you don’t have an existing Resource Group, you can create a new one by clicking on the Create new link.
  • Function App name: You must provide a unique name for your Azure Function App.
  • Publish: Choose the Code option here.
  • Runtime Stack: Make sure to choose the Python option here.
  • Version: Choose the latest version of Python here.
  • Region: You need to choose the Region for your Azure Resource.

Now, after providing all the above details, click the Review + Create button.

How to Create Python Azure Function In Azure Portal

For your information, If you navigate to the Hosting Tab once, you can see that Linux is the only operating system supported now when you are choosing the Runtime stack as Python.

create python azure function

Now it will validate all the details provided by you, Finally, click on the Create button on the next screen to create the Azure Function App.

Now, You can see the Azure Function App is deployed successfully. Click the Go to Resource button to navigate the Azure Function App.

azure function app python example

So Our Azure Function App is ready now, the next thing we need is to create the Azure Function. On the Function App page, click on the Functions from the left navigation and then click the + Add button to create the Azure Function.

Develop Python Azure Function Azure Portal

On the Add function window, choose the Development environment as Develop in the portal, Select the template as HTTP trigger, Provide a name for the new function and Authorization level as Function, and finally, click on the Add button to create the new function.

azure function example python

Now you can see the Azure Python Function created successfully

python azure functions

This is the way to write Python Azure Function In Azure Portal

You may also like following the below Articles

Wrapping Up

In this article, we discussed Azure Function Python Example, Azure Functions In Python, and Deploy Python Azure Function To Azure From Visual Studio Code, Create Python Azure Function In Azure Portal. I hope you have enjoyed this Article !!!