title | description | ms.topic | ms.date | ms.devlang | ms.custom | zone_pivot_groups |
---|---|---|---|---|---|---|
Create a Python function using Visual Studio Code - Azure Functions |
Learn how to create a Python function, then publish the local project to serverless hosting in Azure Functions using the Azure Functions extension in Visual Studio Code. |
quickstart |
05/29/2023 |
python |
devx-track-python, mode-api, devdivchpfy22, vscode-azure-extension-update-complete |
python-mode-functions |
In this article, you use Visual Studio Code to create a Python function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
This article covers both Python programming models supported by Azure Functions. Use the selector at the top to choose your programming model.
::: zone pivot="python-mode-configuration"
Note
There is now a v2 programming model for creating Python functions. To create your first function using the new v2 programming model, select v2 at the top of the article. ::: zone-end ::: zone pivot="python-mode-decorators" [!NOTE] The v2 programming model provides a decorator based approach to create functions. To learn more about the v2 programming model, see the Developer Reference Guide. ::: zone-end
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
There's also a CLI-based version of this article.
Before you begin, make sure that you have the following requirements in place:
- An Azure account with an active subscription. Create an account for free.
::: zone pivot="python-mode-configuration"
-
The Azure Functions Core Tools version 4.x. ::: zone-end ::: zone pivot="python-mode-decorators"
-
The Azure Functions Core Tools, version 4.0.4785 or a later version. ::: zone-end
-
Python versions that are supported by Azure Functions. For more information, see How to install Python.
-
Visual Studio Code on one of the supported platforms.
-
The Python extension for Visual Studio Code.
::: zone pivot="python-mode-configuration"
-
The Azure Functions extension for Visual Studio Code, version 1.8.3 or a later version. ::: zone-end ::: zone pivot="python-mode-decorators"
-
The Azure Functions extension for Visual Studio Code, version 1.8.1 or later.
-
The Azurite V3 extension local storage emulator. While you can also use an actual Azure storage account, this article assumes you're using the Azurite emulator. ::: zone-end
[!INCLUDE functions-x86-emulation-on-arm64-note]
In this section, you use Visual Studio Code to create a local Azure Functions project in Python. Later in this article, you'll publish your function code to Azure.
-
Choose the Azure icon in the Activity bar. Then in the Workspace (local) area, select the + button, choose Create Function in the dropdown. When prompted, choose Create new project.
:::image type="content" source="./media/functions-create-first-function-vs-code/create-new-project.png" alt-text="Screenshot of create a new project window.":::
-
Choose the directory location for your project workspace and choose Select. You should either create a new folder or choose an empty folder for the project workspace. Don't choose a project folder that is already part of a workspace. ::: zone pivot="python-mode-configuration"
-
Provide the following information at the prompts:
Prompt Selection Select a language Choose Python
.Select a Python interpreter to create a virtual environment Choose your preferred Python interpreter. If an option isn't shown, type in the full path to your Python binary. Select a template for your project's first function Choose HTTP trigger
.Provide a function name Enter HttpExample
.Authorization level Choose Anonymous
, which lets anyone call your function endpoint. For more information about the authorization level, see Authorization keys. -
Visual Studio Code uses the provided information and generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. For more information about the files that are created, see Generated project files. ::: zone-end ::: zone pivot="python-mode-decorators"
-
Provide the following information at the prompts:
Prompt Selection Select a language Choose Python (Programming Model V2)
.Select a Python interpreter to create a virtual environment Choose your preferred Python interpreter. If an option isn't shown, type in the full path to your Python binary. Select a template for your project's first function Choose HTTP trigger
.Name of the function you want to create Enter HttpExample
.Authorization level Choose ANONYMOUS
, which lets anyone call your function endpoint. For more information about the authorization level, see Authorization keys. -
Visual Studio Code uses the provided information and generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. The generated
function_app.py
project file contains your functions. -
Open the local.settings.json project file and verify that the
AzureWebJobsFeatureFlags
setting has a value ofEnableWorkerIndexing
. This is required for Functions to interpret your project correctly as the Python v2 model. You'll add this same setting to your application settings after you publish your project to Azure. -
In the local.settings.json file, update the
AzureWebJobsStorage
setting as in the following example:"AzureWebJobsStorage": "UseDevelopmentStorage=true",
This tells the local Functions host to use the storage emulator for the storage connection currently required by the Python v2 model. When you publish your project to Azure, you'll need to instead use the default storage account. If you're instead using an Azure Storage account, set your storage account connection string here.
-
In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select
Azurite: Start
. -
Check the bottom bar and verify that Azurite emulation services are running. If so, you can now run your function locally. ::: zone-end
[!INCLUDE functions-run-function-test-local-vs-code]
After you've verified that the function runs correctly on your local computer, it's time to use Visual Studio Code to publish the project directly to Azure.
[!INCLUDE functions-sign-in-vs-code]
In this section, you create a function app and related resources in your Azure subscription.
-
Choose the Azure icon in the Activity bar. Then in the Resources area, select the + icon and choose the Create Function App in Azure option.
-
Provide the following information at the prompts:
Prompt Selection Select subscription Choose the subscription to use. You won't see this prompt when you have only one subscription visible under Resources. Enter a globally unique name for the function app Type a name that is valid in a URL path. The name you type is validated to make sure that it's unique in Azure Functions. Select a runtime stack Choose the language version on which you've been running locally. Select a location for new resources Choose a region for your function app. ::: zone pivot="python-mode-decorators" ::: zone-end
The extension shows the status of individual resources as they're being created in Azure in the Azure: Activity Log panel.
-
When the creation is complete, the following Azure resources are created in your subscription. The resources are named based on your function app name:
[!INCLUDE functions-vs-code-created-resources]
A notification is displayed after your function app is created and the deployment package is applied.
[!INCLUDE functions-vs-code-create-tip]
[!INCLUDE functions-deploy-project-vs-code]
::: zone pivot="python-mode-decorators"
To use the Python v2 model in your function app, you need to add a new application setting in Azure named AzureWebJobsFeatureFlags
with a value of EnableWorkerIndexing
. This setting is already in your local.settings.json file.
-
In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select
Azure Functions: Add New Setting...
. -
Choose your new function app, type
AzureWebJobsFeatureFlags
for the new app setting name, and press Enter. -
For the value, type
EnableWorkerIndexing
and press Enter.
The setting added to your new function app, which enables it to run the v2 model in Azure. ::: zone-end
[!INCLUDE functions-vs-code-run-remote]
[!INCLUDE functions-cleanup-resources-vs-code.md]
You have used Visual Studio Code to create a function app with a simple HTTP-triggered function. In the next article, you expand that function by connecting to Azure Storage. To learn more about connecting to other Azure services, see Add bindings to an existing function in Azure Functions.
[!div class="nextstepaction"] Connect to an Azure Storage queue Connect to Azure SQL