How To Create Azure Functions In Python

How To Create Azure Functions In Python using Azure Portal

In this Azure tutorial, we will discuss How To Create Azure Functions In Python. Along with this, we will also discuss a few other topics like Azure Functions In Python and we will also discuss Deploy Python Azure Function To Azure From Visual Studio Code, Create Python Azure Function In Azure Portal, Azure Function Python Import Module, How Do I Install An Azure Module In Python?, What is Azure SDK for Python?.

How To Create Azure Functions In Python? You can able to create Azure Functions in Python using the Visual Studio Code IDE.

  • Create a Python Azure Function Project using Visual Studio Code.
  • Choose the Project location and Python as the language.
  • Select Python Interpreter.
  • Choose the trigger that will invoke your Python Azure Function.
  • Provide a name for your Python Azure Function and choose the Authorization level based on your requirement.
  • Select the runtime stack and the region for your resource.

How To Create Azure Functions In Python

Well, here we will discuss How to create Azure Function in Python. Before moving to the actual topic, we will discuss a bit on What Is Serverless? and what is Azure Function?

What Is Serverless?

When we start thinking of Azure Functions, The first thing that comes to our mind is Serverless Architecture. In the case of serverless architecture, as a developer, you no need to take any headache for the infrastructure. They just need to think of the coding part for the Azure Function. This is the responsibility of the cloud provider to manage the infrastructure where the code can run smoothly without any issue.

What is Azure Function?

Azure Functions is simply a “Functions-as-a-service” that provides the solution to run a few lines of code or that are termed as functions in the cloud environment. You just need to worry about writing the code. No need to take any headache for the whole application or the infrastructure. You can find out more information on Azure Function now.

Azure Functions In Python

Well, like we are creating the Azure Function using C#, JavaScript, etc. The same way you can able to create an Azure Function using the Python as well.

Prerequisites

Before starting the actual development, we should know what are the stuffs needed to Develop Azure Functions 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 Azure Function using Python.

Open the Visual Studio Code and 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 Functions In Python

Now 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

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

Create Azure Functions In Python

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.

Develop Azure Functions In Python

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 requirement. Let’s select the Basic and simplest trigger option that is the HTTP trigger here.

Build Azure Functions In Python

Now, the next step is you need to provide an unique name for your Azure Function like below

How to Build Azure Functions In Python

Now 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 requirement.

How to Develop Azure Functions In Python

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

How to create Azure Functions using 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.

How to devlop Azure Functions using Python

If you have not installed the Python till now, 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 the Python 3.6 or the latest version for your operating system.

How to setup Azure Functions using Python

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

Now you can able to see the Azure Function Project is already created using Python and ready to use. You can able to see the _init._.py class file got 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 up 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 able to 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.

Test Python Azure Function Locally

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

How to Test Python Azure Function Locally

This is the way to create python azure function in visual studio code. So our Python Azure Function is working as expected without any issue. 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.

Deploy Python Azure Function To Azure From Visual Studio Code

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

Deploy Python Azure Function To Azure

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

How to Deploy Python Azure Function To Azure

Select the runtime stack version from the below window

How to publish Python Azure Function To Azure

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.

How to publish Python Azure Function To Azure from Visual Studio Code

This way will help you writing Azure Functions In Python and Deploying your Azure Function In Python.

Create Python Azure Function In Azure Portal

We discussed above, azure function python visual studio code. Here we will discuss How to create Python Azure Function In Azure Portal. Follow the below steps for Creating Python Azure Function Using Azure Portal.

Login to the Azure Portal (https://portal.azure.com/)

Search for the Function App in the Azure Portal and click on the search result

Create Python Azure Function In Azure Portal

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 need to 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 you provide all the above details, then click on the Review + create button.

How to Create Python Azure Function In Azure Portal

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

Writing an Azure Python 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 able to see the Azure Function App is deployed successfully. Click on the Go to resource button to navigate to the Azure Function App.

Develop Python Azure Function In Azure Portal

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 on 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 portal, Select the template as HTTP trigger and then Provide a name for the new function and Authorization level as Function and then finally click on the Add button to create the new function.

How to Develop Python Azure Function Azure Portal

Now you can able to see the Azure Python Function created successfully

How to write Python Azure Function Azure Portal

This is the way for Writing Python Azure Function In Azure Portal

Azure Function Python Import Module

Quite difficult to get an easiest way especially, Python in case of Azure Functions. You can try out below steps

  • Write the Python script on your Azure Functions
  • Now, navigate to the Kudu console type cd your project location, and enter
  • The next step is, Create a virtualenv in this folder (python -m virtualenv mynewvenv)
  • Then Load this (cd mynewvenv/Scripts and call activate.bat). Now you can able to see the shell should be prefixed by (mynewvenv).
  • Now, you need to update pip (python -m pip install -U pip)
  • Now you need to Install your dependencies using pip. (python -m pip install Django)
  • Now You are almost done. Where ever you want to import the script, you need to append the sys.path variable with the env
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'mynewvenv/Lib/site-packages')))

How Do I install An Azure Module In Python?

If you want to install a latest version of Library, you can install using the below commands

pip install azure-storage-blob

You can also use the management libraries

pip install azure-mgmt-storage

You can also mention the specific version if you want to install any specific version, you can use the command like below

pip install azure-storage-blob==version number

and same way for the management script

pip install azure-mgmt-storage==version number

Now, If you want to cross check if the installation has been done successfully or not, you can use the below command

pip show azure-storage-blob

And for the management script, you can use the below command

pip show azure-mgmt-storage

This is how you can able to install an Azure module in Python.

What is Azure SDK for Python?

Azure SDK for Python is a set of libraries that helps to interact with Azure from Python. It also helps you with runtime and data needs. You can install the libraries using the pip install command.

There are different versions that are supported, those are 2.7.x, 3.4.x, 3.5.x, 3.6.x and 3.7.x, 3.8x, etc.

You may also like following the below Articles

Wrapping Up

Well, In this article, we discussed Azure Functions In Python, How To Create Azure Functions In Python and we also discussed Deploy Python Azure Function To Azure From Visual Studio Code, Create Python Azure Function In Azure Portal, Azure Function Python Import Module, How Do I Install An Azure Module In Python?, What is Azure SDK for Python?. Hope you have enjoyed this Article !!!

Leave a Comment