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 |
10/24/2022 |
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.
Note
The Python v2 programming model for Functions is currently in Preview. To learn more about the v2 programming model, see the Developer Reference Guide.
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.Select how you would like to open your project Choose Add to workspace
. -
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 how you would like to open your project Choose Add to workspace
. -
Visual Studio Code uses the provided information and generates an Azure Functions project.
-
Open the generated
function_app.py
project file, which contains your functions. -
Uncomment the
test_function
function, which is an HTTP triggered function. -
Replace the
app.route()
method call with the following code:@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
This code enables your HTTP function endpoint to be called in Azure without having to provide an Authorization keys. Local execution doesn't require authorization keys.
Your function code should now look like the following example:
@app.function_name(name="HttpTrigger1") @app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS) def test_function(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 )
-
Open the local.settings.json project file and updated 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 v2 model. When you publish your project to Azure, you'll 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" In the current v2 programming model preview, choose a region from one of the following locations: France Central, West Central US, North Europe, China East, East US, or North Central US. ::: 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