Create an Azure Function in Python using VS Code

Previously, we discussed how we can create an Azure function in Java using IntelliJ IDEA IDE. In this post, we will learn how to create an Azure function in Python using VS Code (Visual Studio Code IDE). We know that Visual Studio Code is an open-source IDE that can be used to build, run and debug the code in an interactive mode.

Azure functions are used to automate and orchestrate complex problems in an event-driven model. It supports multiple languages for development like Java, C#, Python, F#, Powershell, JavaScript, and TypeScript. Here, we will use Python to create a sample Azure Function.

Set up Visual Studio Code for Azure Function Development

To set up a local development environment to create, test, and deploy an Azure Function using Python and VS Code, we need to follow these steps:

1) Download and Install Visual Studio Code IDE and Python

To download the visual studio code, click here. Once the installer is downloaded, follow the on-screen instructions to install it on your local machine.

Next, open the VS code editor, and the below screen will appear.

VS Code start up screen
VS Code start-up screen

We also need to download and install the latest version of the Python if we do not have installed Python on our local machine. The latest Python installer can be downloaded and installed from here.

2) Install Azure Toolkit for VSCode

Now, we need to find and install the Azure Functions extensions to the VS Code. It enables Azure Function development in the VS Code IDE. To do so, go to Extensions -> Find Azure Functions extension -> Click on Install

Additionally, we can install the Azure Tools extensions that can help to work with Azure resources.

Install Azure Functions and Tools extensions
Install Azure Functions and Tools extensions

It may ask to restart the IDE.

3) Create an Azure Function Project in Python

The above-installed extension enables Azure Function development environment in Visual Studio Code. At the left bottom of the IDE, locate the Azure option and click on that button.

Go to Workspace -> click on the + button -> Click on Create Function.

Create Azure Function Project
Create Azure Function Project

On the next screen, click on the Create a new project button.

Create New Project
Create New Project

Next, provide a location for the new project and click on the Select button.

Set Project location
Set Project location

On the next screen, choose the programming language that we want to use for the Azure function development. Here, we have selected Python.

Choose language as Python
Choose the language as Python

Next, we need to configure the Python Interpreter installed on the local machine.

Select Python Interpreter
Select Python Interpreter

Now, we need to select a trigger for the Azure function. In this demo, we are using HTTP Trigger.

Choose HTTP Trigger
Choose HTTP Trigger

Put a name for the Azure function as below. We have used HTTPTriggerTest as the function name here.

Put function name
Put function name

Next, configure an authorization level for the function. In this demo, we have used Function as the authorization level.

Choose authorization
Choose authorization

Finally, select open in the current window option to open this project.

Open in current window
Open in the current window

The VS Code may ask to explicitly provide the consent of trust. Click on the Trust this folder option.

Add folder to the trusted folder
Add folder to the trusted folder

Finally, we have an Azure Function App created in Visual Studio Code using Python language.

Azure Function Project
Azure Function Project

4) Run and Verify the Azure Function

Now that we have already created an Azure Function, we can run and test this Azure function directly from Visual Studio Code IDE. To run the function, click Run -> Start Debugging.

Run the Python Azure function
Run the Python Azure function

It will start the Azure function API that can listen to a specific port.

Python Azure function run output
Python Azure function run output

To verify this function App, we can hit the URL with the below query string.

http://localhost:7071/api/HttpTriggerTest/?name=Gopal
Run and Verify Azure function
Run and Verify the Azure function

5) Deploy the Azure Function

To deploy the Azure function, right-click on the HTTP function and select the Deploy to Function App option. Choose the resource group and deploy it.

Deploy to function app
Deploy to function app

Thanks for the reading. Please share your inputs in the comment section of this post.

Rate This
[Total: 1 Average: 5]

Leave a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.