You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Quickstart: Create a Python function in Azure from the command line
13
11
14
12
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.
15
13
16
-
This article covers both Python programming models supported by Azure Functions. Use the selector at the top to choose your programming model.
17
-
18
-
>[!NOTE]
19
-
>The Python v2 programming model for Azure Functions provides a decorator-based approach for creating functions. To learn more about the Python v2 programming model, see the [Developer Reference Guide](functions-reference-python.md?pivots=python-mode-decorators).
14
+
This article uses the Python v2 programming model for Azure Functions, which provides a decorator-based approach for creating functions. To learn more about the Python v2 programming model, see the [Developer Reference Guide](functions-reference-python.md?pivots=python-mode-decorators)
20
15
21
16
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
22
17
@@ -35,12 +30,13 @@ Before you begin, you must have the following requirements in place:
35
30
+ The Azure [Az PowerShell module](/powershell/azure/install-azure-powershell) version 5.9.0 or later.
36
31
37
32
+[A Python version supported by Azure Functions](supported-languages.md#languages-by-runtime-version).
38
-
::: zone pivot="python-mode-decorators"
33
+
39
34
+ The [Azurite storage emulator](../storage/common/storage-use-azurite.md?tabs=npm#install-azurite). While you can also use an actual Azure Storage account, the article assumes you're using this emulator.
Use the `func --version` command to make sure your version of Core Tools is at least `4.0.5530`.
39
+
44
40
## <aname="create-venv"></a>Create and activate a virtual environment
45
41
46
42
In a suitable folder, run the following commands to create and activate a virtual environment named `.venv`. Make sure that you're using a [version of Python supported by Azure Functions](supported-languages.md?pivots=programming-language-python#languages-by-runtime-version).
@@ -88,49 +84,7 @@ You run all subsequent commands in this activated virtual environment.
88
84
## Create a local function
89
85
90
86
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.
91
-
::: zone pivot="python-mode-configuration"
92
-
In this section, you create a function project that contains a single function.
93
-
94
-
1. Run the [`func init`](functions-core-tools-reference.md#func-init) command as follows to create a Python functions project in the virtual environment.
95
-
96
-
```console
97
-
func init --python --model V1
98
-
```
99
-
100
-
The environment now contains various files for the project, including configuration 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.
101
-
102
-
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).
103
-
104
-
```console
105
-
func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
106
-
```
107
-
108
-
[`func new`](functions-core-tools-reference.md#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*.
109
-
110
-
1. Run this command to make sure that the Azure Functions library is installed in the environment.
In this section, you create a function project and add an HTTP triggered function.
135
89
136
90
1. Run the [`func init`](functions-core-tools-reference.md#func-init) command as follows to create a Python v2 functions project in the virtual environment.
@@ -148,53 +102,6 @@ In this section, you create a function project and add an HTTP triggered functio
148
102
```
149
103
150
104
If prompted, choose the **ANONYMOUS** option. [`func new`](functions-core-tools-reference.md#func-new) adds an HTTP trigger endpoint named `HttpExample` to the `function_app.py` file, which is accessible without authentication.
151
-
<!--- Remove these last steps after the next Core Tools version is released (4.28.0)--->
152
-
1. Open the local.settings.json project file and verify that the `AzureWebJobsFeatureFlags` setting has a value of `EnableWorkerIndexing`. This setting is required for Functions to interpret your project correctly as the Python v2 model when running locally.
153
-
154
-
1. In the local.settings.json file, update the `AzureWebJobsStorage` setting as in the following example:
This setting 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 need to instead use the default storage account. If you're instead using an Azure Storage account, set your storage account connection string here.
161
-
162
-
1. Run this command to make sure that the Azure Functions library is installed in the environment.
By default, local development uses the Azurite storage emulator. This emulator is used when the `AzureWebJobsStorage` setting in the *local.settings.json* project file is set to `UseDevelopmentStorage=true`. When using the emulator, you must start the local Azurite storage emulator before running the function.
187
-
188
-
You can skip this step if the `AzureWebJobsStorage` setting in *local.settings.json* is set to the connection string for an Azure Storage account instead of `UseDevelopmentStorage=true`.
189
-
190
-
Use the following command to start the Azurite storage emulator in a separate process:
191
-
192
-
```cmd
193
-
start azurite
194
-
```
195
-
196
-
For more information, see [Run Azurite](../storage/common/storage-use-azurite.md?tabs=npm#run-azurite)
@@ -210,14 +117,14 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
210
117
211
118
1. If needed, sign in to Azure.
212
119
213
-
# [Azure CLI](#tab/azure-cli)
120
+
### [Azure CLI](#tab/azure-cli)
214
121
```azurecli
215
122
az login
216
123
```
217
124
218
125
The [`az login`](/cli/azure/reference-index#az-login) command signs you into your Azure account.
219
126
220
-
# [Azure PowerShell](#tab/azure-powershell)
127
+
### [Azure PowerShell](#tab/azure-powershell)
221
128
```azurepowershell
222
129
Connect-AzAccount
223
130
```
@@ -226,18 +133,17 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
226
133
227
134
---
228
135
229
-
230
136
1. Create a resource group named `AzureFunctionsQuickstart-rg` in your chosen region.
231
137
232
-
# [Azure CLI](#tab/azure-cli)
138
+
### [Azure CLI](#tab/azure-cli)
233
139
234
140
```azurecli
235
141
az group create --name AzureFunctionsQuickstart-rg --location <REGION>
236
142
```
237
143
238
144
The [az group create](/cli/azure/group#az-group-create) command creates a resource group. In the above command, replace `<REGION>` 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.
@@ -247,23 +153,20 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
247
153
248
154
---
249
155
250
-
::: zone pivot="python-mode-decorators"
251
-
::: zone-end
252
-
253
156
> [!NOTE]
254
157
> 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.
255
158
256
159
1. Create a general-purpose storage account in your resource group and region.
@@ -279,15 +182,15 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
279
182
280
183
1. Create the function app in Azure.
281
184
282
-
# [Azure CLI](#tab/azure-cli)
185
+
### [Azure CLI](#tab/azure-cli)
283
186
284
187
```azurecli
285
188
az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location westeurope --runtime python --runtime-version <PYTHON_VERSION> --functions-version 4 --name <APP_NAME> --os-type linux --storage-account <STORAGE_NAME>
286
189
```
287
190
288
191
The [az functionapp create](/cli/azure/functionapp#az-functionapp-create) command creates the function app in Azure. You must supply `--os-type linux` because Python functions only run on Linux.
Because your function uses an HTTP trigger, you invoke it by making an HTTP request to its URL in the browser or with a tool like curl.
212
+
213
+
### [Browser](#tab/browser)
214
+
215
+
Copy the complete **Invoke URL** shown in the output of the `publish` command into a browser address bar, appending the query parameter `?name=Functions`. The browser should display similar output as when you ran the function locally.
216
+
217
+
### [curl](#tab/curl)
218
+
219
+
Run [`curl`](https://curl.haxx.se/) with the **Invoke URL** shown in the output of the `publish` command, appending the parameter `?name=Functions`. The output of the command should be the text, "Hello Functions."
220
+
221
+
---
307
222
223
+
<!--- // Re-enable this after this bug gets fixed: https://github.com/Azure/azure-functions-core-tools/issues/3609
308
224
Run the following command to view near real-time streaming logs in Application Insights in the Azure portal.
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.
<!--- Revert this after we get Node.js v4 support added to the follow-on quickstarts -->
174
+
::: zone pivot="nodejs-model-v3"
174
175
You have used [Visual Studio Code](functions-develop-vs-code.md?tabs=javascript) to create a function app with a simple HTTP-triggered function. In the next article, you expand that function by connecting to either Azure Cosmos DB or Azure Storage. To learn more about connecting to other Azure services, see [Add bindings to an existing function in Azure Functions](add-bindings-existing-function.md?tabs=javascript). If you want to learn more about security, see [Securing Azure Functions](security-concepts.md).
175
176
176
177
> [!div class="nextstepaction"]
177
178
> [Connect to Azure Cosmos DB](functions-add-output-binding-cosmos-db-vs-code.md?pivots=programming-language-javascript)
178
179
> [!div class="nextstepaction"]
179
180
> [Connect to Azure Queue Storage](functions-add-output-binding-storage-queue-vs-code.md?pivots=programming-language-javascript)
181
+
::: zone-end
182
+
::: zone pivot="nodejs-model-v4"
183
+
You have used [Visual Studio Code](functions-develop-vs-code.md?tabs=javascript) to create a function app with a simple HTTP-triggered function.
180
184
185
+
> [!div class="nextstepaction"]
186
+
> [Learn more about JavaScript functions](./functions-reference-node.md)
0 commit comments