Skip to content

Commit 66fb988

Browse files
authored
Merge pull request #261247 from ggailey777/fixup
[Functions][Python] CLI quickstart updates and fixes
2 parents b673778 + 3a05e16 commit 66fb988

File tree

1 file changed

+88
-39
lines changed

1 file changed

+88
-39
lines changed

articles/azure-functions/create-first-function-cli-python.md

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Create a Python function from the command line - Azure Functions
33
description: Learn how to create a Python function from the command line, then publish the local project to serverless hosting in Azure Functions.
4-
ms.date: 08/07/2023
4+
ms.date: 12/14/2023
55
ms.topic: quickstart
66
ms.devlang: python
77
ms.custom: devx-track-python, devx-track-azurecli, devx-track-azurepowershell, mode-api, devdivchpfy22
@@ -16,7 +16,7 @@ In this article, you use command-line tools to create a Python function that res
1616
This article covers both Python programming models supported by Azure Functions. Use the selector at the top to choose your programming model.
1717

1818
>[!NOTE]
19-
>The v2 programming model provides a decorator based 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).
2020
2121
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
2222

@@ -43,9 +43,9 @@ Before you begin, you must have the following requirements in place:
4343

4444
## <a name="create-venv"></a>Create and activate a virtual environment
4545

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).
4747

48-
# [bash](#tab/bash)
48+
### [bash](#tab/bash)
4949

5050
```bash
5151
python -m venv .venv
@@ -61,7 +61,7 @@ If Python didn't install the venv package on your Linux distribution, run the fo
6161
sudo apt-get install python3-venv
6262
```
6363

64-
# [PowerShell](#tab/powershell)
64+
### [PowerShell](#tab/powershell)
6565

6666
```powershell
6767
py -m venv .venv
@@ -71,7 +71,7 @@ py -m venv .venv
7171
.venv\scripts\activate
7272
```
7373

74-
# [Cmd](#tab/cmd)
74+
### [Cmd](#tab/cmd)
7575

7676
```cmd
7777
py -m venv .venv
@@ -91,19 +91,13 @@ In Azure Functions, a function project is a container for one or more individual
9191
::: zone pivot="python-mode-configuration"
9292
In this section, you create a function project that contains a single function.
9393

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.
9595

9696
```console
97-
func init LocalFunctionProj --python
97+
func init --python
9898
```
9999

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.
107101

108102
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).
109103

@@ -112,36 +106,70 @@ In this section, you create a function project that contains a single function.
112106
```
113107

114108
`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.
111+
112+
### [bash](#tab/bash)
113+
114+
```bash
115+
.venv/bin/python -m pip install -r requirements.txt
116+
```
117+
118+
### [PowerShell](#tab/powershell)
119+
120+
```powershell
121+
.venv\Scripts\python -m pip install -r requirements.txt
122+
```
123+
124+
### [Cmd](#tab/cmd)
125+
126+
```cmd
127+
.venv\Scripts\python -m pip install -r requirements.txt
128+
```
129+
130+
---
131+
115132
::: zone-end
116133
::: zone pivot="python-mode-decorators"
117134
In this section, you create a function project and add an HTTP triggered function.
118135

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.
120137

121138
```console
122-
func init LocalFunctionProj --python -m V2
139+
func init --python -m V2
123140
```
124141

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.
126143

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
130149
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`:
134-
135-
```python
136-
import azure.functions as func
137-
138-
app = func.FunctionApp()
139-
140-
@app.function_name(name="HttpExample")
141-
@app.route(route="hello")
142-
def test_function(req: func.HttpRequest) -> func.HttpResponse:
143-
return func.HttpResponse("HttpExample function processed a request!")
144-
```
150+
app = func.FunctionApp()
151+
152+
@app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)
153+
def test_function(req: func.HttpRequest) -> func.HttpResponse:
154+
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+
```
145173
146174
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.
147175

@@ -152,18 +180,39 @@ In this section, you create a function project and add an HTTP triggered functio
152180
```
153181

154182
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.
185+
186+
### [bash](#tab/bash)
187+
188+
```bash
189+
.venv/bin/python -m pip install -r requirements.txt
190+
```
191+
192+
### [PowerShell](#tab/powershell)
193+
194+
```powershell
195+
.venv\Scripts\python -m pip install -r requirements.txt
196+
```
197+
198+
### [Cmd](#tab/cmd)
199+
200+
```cmd
201+
.venv\Scripts\python -m pip install -r requirements.txt
202+
```
203+
204+
---
205+
157206
## Start the storage emulator
158207

159208
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.
160209

161210
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`.
162211

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:
164213

165214
```cmd
166-
azurite
215+
start azurite
167216
```
168217

169218
For more information, see [Run Azurite](../storage/common/storage-use-azurite.md?tabs=npm#run-azurite)

0 commit comments

Comments
 (0)