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
@@ -16,7 +16,7 @@ In this article, you use command-line tools to create a Python function that res
16
16
This article covers both Python programming models supported by Azure Functions. Use the selector at the top to choose your programming model.
17
17
18
18
>[!NOTE]
19
-
>The v2 programming model provides a decoratorbased approach to create functions. To learn more about the Python v2 programming model, see the [Developer Reference Guide](functions-reference-python.md).
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).
20
20
21
21
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
22
22
@@ -43,9 +43,9 @@ Before you begin, you must have the following requirements in place:
43
43
44
44
## <aname="create-venv"></a>Create and activate a virtual environment
45
45
46
-
In a suitable folder, run the following commands to create and activate a virtual environment named `.venv`. Make sure that you're using Python 3.9, 3.8, or 3.7, which are supported by Azure Functions.
46
+
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 that is supported by Azure Functions](supported-languages.md?pivots=programming-language-python#languages-by-runtime-version).
47
47
48
-
# [bash](#tab/bash)
48
+
###[bash](#tab/bash)
49
49
50
50
```bash
51
51
python -m venv .venv
@@ -61,7 +61,7 @@ If Python didn't install the venv package on your Linux distribution, run the fo
61
61
sudo apt-get install python3-venv
62
62
```
63
63
64
-
# [PowerShell](#tab/powershell)
64
+
###[PowerShell](#tab/powershell)
65
65
66
66
```powershell
67
67
py -m venv .venv
@@ -71,7 +71,7 @@ py -m venv .venv
71
71
.venv\scripts\activate
72
72
```
73
73
74
-
# [Cmd](#tab/cmd)
74
+
###[Cmd](#tab/cmd)
75
75
76
76
```cmd
77
77
py -m venv .venv
@@ -91,19 +91,13 @@ In Azure Functions, a function project is a container for one or more individual
91
91
::: zone pivot="python-mode-configuration"
92
92
In this section, you create a function project that contains a single function.
93
93
94
-
1. Run the `func init` command as follows to create a functions project in a folder named *LocalFunctionProj* with the specified runtime.
94
+
1. Run the `func init` command as follows to create a Python functions project in the virtual environment.
95
95
96
96
```console
97
-
func init LocalFunctionProj --python
97
+
func init --python
98
98
```
99
99
100
-
1. Go to the project folder.
101
-
102
-
```console
103
-
cd LocalFunctionProj
104
-
```
105
-
106
-
This folder 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.
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.
107
101
108
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).
109
103
@@ -112,36 +106,70 @@ In this section, you create a function project that contains a single function.
112
106
```
113
107
114
108
`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.
118
135
119
-
1. Run the `func init` command as follows to create a functions project in a folder named *LocalFunctionProj* with the specified runtime and the specified programming model version.
136
+
1. Run the `func init` command as follows to create a Python v2 functions project in the virtual environment.
120
137
121
138
```console
122
-
func init LocalFunctionProj --python -m V2
139
+
func init --python -m V2
123
140
```
124
141
125
-
1. Go to the project folder.
142
+
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.
126
143
127
-
```console
128
-
cd LocalFunctionProj
129
-
```
144
+
1. The file `function_app.py` can include all functions within your project. Open this file and replace the existing contents with the following code that defines an HTTP triggered function endpoint named `HttpExample`:
145
+
146
+
```python
147
+
import azure.functions as func
148
+
import logging
130
149
131
-
This folder 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.
132
-
133
-
1. The file `function_app.py` can include all functions within your project. Open this file and replace the existing contents with the following code that adds an HTTP triggered function named `HttpExample`:
logging.info('Python HTTP trigger function processed a request.')
155
+
156
+
name = req.params.get('name')
157
+
if not name:
158
+
try:
159
+
req_body = req.get_json()
160
+
except ValueError:
161
+
pass
162
+
else:
163
+
name = req_body.get('name')
164
+
165
+
if name:
166
+
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
167
+
else:
168
+
return func.HttpResponse(
169
+
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
170
+
status_code=200
171
+
)
172
+
```
145
173
146
174
1. Open the local.settings.json project file and verify that the `AzureWebJobsFeatureFlags` setting has a value of `EnableWorkerIndexing`. 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.
147
175
@@ -152,18 +180,39 @@ In this section, you create a function project and add an HTTP triggered functio
152
180
```
153
181
154
182
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.
155
-
::: zone-end
156
-
::: zone pivot="python-mode-decorators"
183
+
184
+
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.
160
209
161
210
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`.
162
211
163
-
Use the following command to start the Azurite storage emulator:
212
+
Use the following command to start the Azurite storage emulator in a separate process:
164
213
165
214
```cmd
166
-
azurite
215
+
start azurite
167
216
```
168
217
169
218
For more information, see [Run Azurite](../storage/common/storage-use-azurite.md?tabs=npm#run-azurite)
0 commit comments