--- title: Create a Python function from the command line - Azure Functions description: Learn how to create a Python function from the command line, then publish the local project to serverless hosting in Azure Functions. ms.date: 11/03/2020 ms.topic: quickstart ms.devlang: python ms.custom: devx-track-python, devx-track-azurecli, devx-track-azurepowershell, mode-api adobe-target: true adobe-target-activity: DocsExp–386541–A/B–Enhanced-Readability-Quickstarts–2.19.2021 adobe-target-experience: Experience B adobe-target-content: ./create-first-function-cli-python-uiex --- # Quickstart: Create a Python function in Azure from the command line [!INCLUDE [functions-language-selector-quickstart-cli](../../includes/functions-language-selector-quickstart-cli.md)] In this article, you use command-line tools 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. Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account. There is also a [Visual Studio Code-based version](create-first-function-vs-code-python.md) of this article. ## Configure your local environment Before you begin, you must have the following: + An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio). + The [Azure Functions Core Tools](functions-run-local.md#v2) version 4.x. + One of the following tools for creating Azure resources: + [Azure CLI](/cli/azure/install-azure-cli) version 2.4 or later. + The Azure [Az PowerShell module](/powershell/azure/install-az-ps) version 5.9.0 or later. + [Python versions that are supported by Azure Functions](supported-languages.md#languages-by-runtime-version) ### Prerequisite check Verify your prerequisites, which depend on whether you are using Azure CLI or Azure PowerShell for creating Azure resources: # [Azure CLI](#tab/azure-cli) + In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 4.x. + Run `az --version` to check that the Azure CLI version is 2.4 or later. + Run `az login` to sign in to Azure and verify an active subscription. + Run `python --version` (Linux/macOS) or `py --version` (Windows) to check your Python version reports 3.9.x, 3.8.x, or 3.7.x. # [Azure PowerShell](#tab/azure-powershell) + In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 4.x. + Run `(Get-Module -ListAvailable Az).Version` and verify version 5.0 or later. + Run `Connect-AzAccount` to sign in to Azure and verify an active subscription. + Run `python --version` (Linux/macOS) or `py --version` (Windows) to check your Python version reports 3.9.x, 3.8.x, or 3.7.x. --- ## Create and activate a virtual environment In a suitable folder, run the following commands to create and activate a virtual environment named `.venv`. Be sure to use Python 3.8, 3.7 or 3.6, which are supported by Azure Functions. # [bash](#tab/bash) ```bash python -m venv .venv ``` ```bash source .venv/bin/activate ``` If Python didn't install the venv package on your Linux distribution, run the following command: ```bash sudo apt-get install python3-venv ``` # [PowerShell](#tab/powershell) ```powershell py -m venv .venv ``` ```powershell .venv\scripts\activate ``` # [Cmd](#tab/cmd) ```cmd py -m venv .venv ``` ```cmd .venv\scripts\activate ``` --- You run all subsequent commands in this activated virtual environment. ## Create a local function project In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function. 1. Run the `func init` command, as follows, to create a functions project in a folder named *LocalFunctionProj* with the specified runtime: ```console func init LocalFunctionProj --python ``` 1. Navigate into the project folder: ```console cd LocalFunctionProj ``` This folder contains various files for the project, including configurations files named [local.settings.json](functions-develop-local.md#local-settings-file) and [host.json](functions-host-json.md). Because *local.settings.json* can contain secrets downloaded from Azure, the file is excluded from source control by default in the *.gitignore* file. 1. Add a function to your project by using the following command, where the `--name` argument is the unique name of your function (HttpExample) and the `--template` argument specifies the function's trigger (HTTP). ```console func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous" ``` `func new` creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named *function.json*. Get the list of templates by using the following command. ```console func templates list -l python ``` ### (Optional) Examine the file contents If desired, you can skip to [Run the function locally](#run-the-function-locally) and examine the file contents later. #### \_\_init\_\_.py *\_\_init\_\_.py* contains a `main()` Python function that's triggered according to the configuration in *function.json*. :::code language="python" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/__init__.py"::: For an HTTP trigger, the function receives request data in the variable `req` as defined in *function.json*. `req` is an instance of the [azure.functions.HttpRequest class](/python/api/azure-functions/azure.functions.httprequest). The return object, defined as `$return` in *function.json*, is an instance of [azure.functions.HttpResponse class](/python/api/azure-functions/azure.functions.httpresponse). To learn more, see [Azure Functions HTTP triggers and bindings](./functions-bindings-http-webhook.md?tabs=python). #### function.json *function.json* is a configuration file that defines the input and output `bindings` for the function, including the trigger type. You can change `scriptFile` to invoke a different Python file if desired. :::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/function.json"::: Each binding requires a direction, a type, and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook-trigger.md) and output binding of type [`http`](functions-bindings-http-webhook-output.md). [!INCLUDE [functions-run-function-test-local-cli](../../includes/functions-run-function-test-local-cli.md)] ## Create supporting Azure resources for your function Before you can deploy your function code to Azure, you need to create three resources: - A resource group, which is a logical container for related resources. - A Storage account, which maintains state and other information about your projects. - A function app, which provides the environment for executing your function code. A function app maps to your local function project and lets you group functions as a logical unit for easier management, deployment, and sharing of resources. Use the following commands to create these items. Both Azure CLI and PowerShell are supported. 1. If you haven't done so already, sign in to Azure: # [Azure CLI](#tab/azure-cli) ```azurecli az login ``` The [az login](/cli/azure/reference-index#az_login) command signs you into your Azure account. # [Azure PowerShell](#tab/azure-powershell) ```azurepowershell Connect-AzAccount ``` The [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount) cmdlet signs you into your Azure account. --- 1. When using the Azure CLI, you can turn on the `param-persist` option that automatically tracks the names of your created resources. To learn more, see [Azure CLI persisted parameter](/cli/azure/param-persist-howto). # [Azure CLI](#tab/azure-cli) ```azurecli az config param-persist on ``` # [Azure PowerShell](#tab/azure-powershell) This feature isn't available in Azure PowerShell. --- 1. Create a resource group named `AzureFunctionsQuickstart-rg` in your chosen region: # [Azure CLI](#tab/azure-cli) ```azurecli az group create --name AzureFunctionsQuickstart-rg --location ``` The [az group create](/cli/azure/group#az_group_create) command creates a resource group. In the above command, replace `` with a region near you, using an available region code returned from the [az account list-locations](/cli/azure/account#az_account_list_locations) command. # [Azure PowerShell](#tab/azure-powershell) ```azurepowershell New-AzResourceGroup -Name AzureFunctionsQuickstart-rg -Location '' ``` The [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup) command creates a resource group. You generally create your resource group and resources in a region near you, using an available region returned from the [Get-AzLocation](/powershell/module/az.resources/get-azlocation) cmdlet. --- > [!NOTE] > You can't host Linux and Windows apps in the same resource group. If you have an existing resource group named `AzureFunctionsQuickstart-rg` with a Windows function app or web app, you must use a different resource group. 1. Create a general-purpose storage account in your resource group and region: # [Azure CLI](#tab/azure-cli) ```azurecli az storage account create --name --sku Standard_LRS ``` The [az storage account create](/cli/azure/storage/account#az_storage_account_create) command creates the storage account. # [Azure PowerShell](#tab/azure-powershell) ```azurepowershell New-AzStorageAccount -ResourceGroupName AzureFunctionsQuickstart-rg -Name -SkuName Standard_LRS -Location ``` The [New-AzStorageAccount](/powershell/module/az.storage/new-azstorageaccount) cmdlet creates the storage account. --- In the previous example, replace `` with a name that is appropriate to you and unique in Azure Storage. Names must contain three to 24 characters numbers and lowercase letters only. `Standard_LRS` specifies a general-purpose account, which is [supported by Functions](storage-considerations.md#storage-account-requirements). The storage account incurs only a few cents (USD) for this quickstart. 1. Create the function app in Azure: # [Azure CLI](#tab/azure-cli) ```azurecli az functionapp create --consumption-plan-location westeurope --runtime python --runtime-version 3.8 --functions-version 3 --name --os-type linux --storage-account ``` The [az functionapp create](/cli/azure/functionapp#az_functionapp_create) command creates the function app in Azure. If you are using Python 3.7 or 3.6, change `--runtime-version` to `3.7` or `3.6`, respectively. You must supply `--os-type linux` because Python functions can't run on Windows, which is the default. # [Azure PowerShell](#tab/azure-powershell) ```azurepowershell New-AzFunctionApp -Name -ResourceGroupName AzureFunctionsQuickstart-rg -StorageAccount -FunctionsVersion 3 -RuntimeVersion 3.8 -Runtime python -Location '' ``` The [New-AzFunctionApp](/powershell/module/az.functions/new-azfunctionapp) cmdlet creates the function app in Azure. If you're using Python 3.7 or 3.6, change `-RuntimeVersion` to `3.7` or `3.6`, respectively. --- In the previous example, replace `` with a globally unique name appropriate to you. The `` is also the default DNS domain for the function app. This command creates a function app running in your specified language runtime under the [Azure Functions Consumption Plan](consumption-plan.md), which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see [Monitor Azure Functions](functions-monitoring.md). The instance incurs no costs until you activate it. [!INCLUDE [functions-publish-project-cli](../../includes/functions-publish-project-cli.md)] [!INCLUDE [functions-run-remote-azure-cli](../../includes/functions-run-remote-azure-cli.md)] Run the following command to view near real-time [streaming logs](functions-run-local.md#enable-streaming-logs) in Application Insights in the Azure portal: ```console func azure functionapp logstream --browser ``` In a separate terminal window or in the browser, call the remote function again. A verbose log of the function execution in Azure is shown in the terminal. [!INCLUDE [functions-cleanup-resources-cli](../../includes/functions-cleanup-resources-cli.md)] ## Next steps > [!div class="nextstepaction"] > [Connect to an Azure Storage queue](functions-add-output-binding-storage-queue-cli.md?pivots=programming-language-python) [Having issues? Let us know.](https://aka.ms/python-functions-qs-survey)