Skip to content

Commit e952be4

Browse files
author
Hanzhang Zeng (Roger)
committed
Add link to developer reference
1 parent 74a7696 commit e952be4

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

articles/azure-functions/functions-reference-python.md

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
title: Python developer reference for Azure Functions
2+
title: Python developer reference for Azure Functions
33
description: Understand how to develop functions with Python
44
ms.topic: article
55
ms.date: 12/13/2019
66
---
77

88
# Azure Functions Python developer guide
99

10-
This article is an introduction to developing Azure Functions using Python. The content below assumes that you've already read the [Azure Functions developers guide](functions-reference.md).
10+
This article is an introduction to developing Azure Functions using Python. The content below assumes that you've already read the [Azure Functions developers guide](functions-reference.md).
1111

12-
For standalone Function sample projects in Python, see the [Python Functions samples](/samples/browse/?products=azure-functions&languages=python).
12+
For standalone Function sample projects in Python, see the [Python Functions samples](/samples/browse/?products=azure-functions&languages=python).
1313

1414
## Programming model
1515

@@ -82,7 +82,7 @@ The main project folder (\_\_app\_\_) can contain the following files:
8282
* *.funcignore*: (Optional) declares files that shouldn't get published to Azure.
8383
* *.gitignore*: (Optional) declares files that are excluded from a git repo, such as local.settings.json.
8484

85-
Each function has its own code file and binding configuration file (function.json).
85+
Each function has its own code file and binding configuration file (function.json).
8686

8787
When deploying your project to a function app in Azure, the entire contents of the main project (*\_\_app\_\_*) folder should be included in the package, but not the folder itself. We recommend that you maintain your tests in a folder separate from the project folder, in this example `tests`. This keeps you from deploying test code with your app. For more information, see [Unit Testing](#unit-testing).
8888

@@ -128,7 +128,7 @@ from __app__.shared_code import my_first_helper_function
128128

129129
## Triggers and Inputs
130130

131-
Inputs are divided into two categories in Azure Functions: trigger input and additional input. Although they are different in the `function.json` file, usage is identical in Python code. Connection strings or secrets for trigger and input sources map to values in the `local.settings.json` file when running locally, and the application settings when running in Azure.
131+
Inputs are divided into two categories in Azure Functions: trigger input and additional input. Although they are different in the `function.json` file, usage is identical in Python code. Connection strings or secrets for trigger and input sources map to values in the `local.settings.json` file when running locally, and the application settings when running in Azure.
132132

133133
For example, the following code demonstrates the difference between the two:
134134

@@ -255,12 +255,12 @@ To learn more about logging, see [Monitor Azure Functions](functions-monitoring.
255255

256256
## HTTP Trigger and bindings
257257

258-
The HTTP trigger is defined in the function.jon file. The `name` of the binding must match the named parameter in the function.
258+
The HTTP trigger is defined in the function.jon file. The `name` of the binding must match the named parameter in the function.
259259
In the previous examples, a binding name `req` is used. This parameter is an [HttpRequest] object, and an [HttpResponse] object is returned.
260260

261-
From the [HttpRequest] object, you can get request headers, query parameters, route parameters, and the message body.
261+
From the [HttpRequest] object, you can get request headers, query parameters, route parameters, and the message body.
262262

263-
The following example is from the [HTTP trigger template for Python](https://github.com/Azure/azure-functions-templates/tree/dev/Functions.Templates/Templates/HttpTrigger-Python).
263+
The following example is from the [HTTP trigger template for Python](https://github.com/Azure/azure-functions-templates/tree/dev/Functions.Templates/Templates/HttpTrigger-Python).
264264

265265
```python
266266
def main(req: func.HttpRequest) -> func.HttpResponse:
@@ -274,7 +274,7 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
274274
pass
275275
else:
276276
name = req_body.get('name')
277-
277+
278278
if name:
279279
return func.HttpResponse(f"Hello {name}!", headers=headers)
280280
else:
@@ -284,7 +284,7 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
284284
)
285285
```
286286

287-
In this function, the value of the `name` query parameter is obtained from the `params` parameter of the [HttpRequest] object. The JSON-encoded message body is read using the `get_json` method.
287+
In this function, the value of the `name` query parameter is obtained from the `params` parameter of the [HttpRequest] object. The JSON-encoded message body is read using the `get_json` method.
288288

289289
Likewise, you can set the `status_code` and `headers` for the response message in the returned [HttpResponse] object.
290290

@@ -322,13 +322,13 @@ def main():
322322

323323
### Use multiple language worker processes
324324

325-
By default, every Functions host instance has a single language worker process. You can increase the number of worker processes per host (up to 10) by using the [FUNCTIONS_WORKER_PROCESS_COUNT](functions-app-settings.md#functions_worker_process_count) application setting. Azure Functions then tries to evenly distribute simultaneous function invocations across these workers.
325+
By default, every Functions host instance has a single language worker process. You can increase the number of worker processes per host (up to 10) by using the [FUNCTIONS_WORKER_PROCESS_COUNT](functions-app-settings.md#functions_worker_process_count) application setting. Azure Functions then tries to evenly distribute simultaneous function invocations across these workers.
326326

327-
The FUNCTIONS_WORKER_PROCESS_COUNT applies to each host that Functions creates when scaling out your application to meet demand.
327+
The FUNCTIONS_WORKER_PROCESS_COUNT applies to each host that Functions creates when scaling out your application to meet demand.
328328

329329
## Context
330330

331-
To get the invocation context of a function during execution, include the [`context`](/python/api/azure-functions/azure.functions.context?view=azure-python) argument in its signature.
331+
To get the invocation context of a function during execution, include the [`context`](/python/api/azure-functions/azure.functions.context?view=azure-python) argument in its signature.
332332

333333
For example:
334334

@@ -343,18 +343,18 @@ def main(req: azure.functions.HttpRequest,
343343

344344
The [**Context**](/python/api/azure-functions/azure.functions.context?view=azure-python) class has the following string attributes:
345345

346-
`function_directory`
346+
`function_directory`
347347
The directory in which the function is running.
348348

349-
`function_name`
349+
`function_name`
350350
Name of the function.
351351

352-
`invocation_id`
352+
`invocation_id`
353353
ID of the current function invocation.
354354

355355
## Global variables
356356

357-
It is not guaranteed that the state of your app will be preserved for future executions. However, the Azure Functions runtime often reuses the same process for multiple executions of the same app. In order to cache the results of an expensive computation, declare it as a global variable.
357+
It is not guaranteed that the state of your app will be preserved for future executions. However, the Azure Functions runtime often reuses the same process for multiple executions of the same app. In order to cache the results of an expensive computation, declare it as a global variable.
358358

359359
```python
360360
CACHED_DATA = None
@@ -386,9 +386,9 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
386386
logging.info(f'My app setting value:{my_app_setting_value}')
387387
```
388388

389-
For local development, application settings are [maintained in the local.settings.json file](functions-run-local.md#local-settings-file).
389+
For local development, application settings are [maintained in the local.settings.json file](functions-run-local.md#local-settings-file).
390390

391-
## Python version
391+
## Python version
392392

393393
Azure Functions supports the following Python versions:
394394

@@ -399,13 +399,13 @@ Azure Functions supports the following Python versions:
399399

400400
<sup>*</sup>Official CPython distributions
401401

402-
To request a specific Python version when you create your function app in Azure, use the `--runtime-version` option of the [`az functionapp create`](/cli/azure/functionapp#az-functionapp-create) command. The Functions runtime version is set by the `--functions-version` option. The Python version is set when the function app is created and can't be changed.
402+
To request a specific Python version when you create your function app in Azure, use the `--runtime-version` option of the [`az functionapp create`](/cli/azure/functionapp#az-functionapp-create) command. The Functions runtime version is set by the `--functions-version` option. The Python version is set when the function app is created and can't be changed.
403403

404-
When running locally, the runtime uses the available Python version.
404+
When running locally, the runtime uses the available Python version.
405405

406406
## Package management
407407

408-
When developing locally using the Azure Functions Core Tools or Visual Studio Code, add the names and versions of the required packages to the `requirements.txt` file and install them using `pip`.
408+
When developing locally using the Azure Functions Core Tools or Visual Studio Code, add the names and versions of the required packages to the `requirements.txt` file and install them using `pip`.
409409

410410
For example, the following requirements file and pip command can be used to install the `requests` package from PyPI.
411411

@@ -419,41 +419,41 @@ pip install -r requirements.txt
419419

420420
## Publishing to Azure
421421

422-
When you're ready to publish, make sure that all your publicly available dependencies are listed in the requirements.txt file, which is located at the root of your project directory.
422+
When you're ready to publish, make sure that all your publicly available dependencies are listed in the requirements.txt file, which is located at the root of your project directory.
423423

424424
Project files and folders that are excluded from publishing, including the virtual environment folder, are listed in the .funcignore file.
425425

426426
There are three build actions supported for publishing your Python project to Azure:
427427

428-
+ Remote build: Dependencies are obtained remotely based on the contents of the requirements.txt file. [Remote build](functions-deployment-technologies.md#remote-build) is the recommended build method. Remote is also the default build option of Azure tooling.
429-
+ Local build: Dependencies are obtained locally based on the contents of the requirements.txt file.
428+
+ Remote build: Dependencies are obtained remotely based on the contents of the requirements.txt file. [Remote build](functions-deployment-technologies.md#remote-build) is the recommended build method. Remote is also the default build option of Azure tooling.
429+
+ Local build: Dependencies are obtained locally based on the contents of the requirements.txt file.
430430
+ Custom dependencies: Your project uses packages not publicly available to our tools. (Requires Docker.)
431431

432432
To build your dependencies and publish using a continuous delivery (CD) system, [use Azure Pipelines](functions-how-to-azure-devops.md).
433433

434434
### Remote build
435435

436-
By default, the Azure Functions Core Tools requests a remote build when you use the following [func azure functionapp publish](functions-run-local.md#publish) command to publish your Python project to Azure.
436+
By default, the Azure Functions Core Tools requests a remote build when you use the following [func azure functionapp publish](functions-run-local.md#publish) command to publish your Python project to Azure.
437437

438438
```bash
439439
func azure functionapp publish <APP_NAME>
440440
```
441441

442442
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
443443

444-
The [Azure Functions Extension for Visual Studio Code](functions-create-first-function-vs-code.md#publish-the-project-to-azure) also requests a remote build by default.
444+
The [Azure Functions Extension for Visual Studio Code](functions-create-first-function-vs-code.md#publish-the-project-to-azure) also requests a remote build by default.
445445

446446
### Local build
447447

448-
You can prevent doing a remote build by using the following [func azure functionapp publish](functions-run-local.md#publish) command to publish with a local build.
448+
You can prevent doing a remote build by using the following [func azure functionapp publish](functions-run-local.md#publish) command to publish with a local build.
449449

450450
```command
451451
func azure functionapp publish <APP_NAME> --build local
452452
```
453453

454-
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
454+
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
455455

456-
Using the `--build local` option, project dependencies are read from the requirements.txt file and those dependent packages are downloaded and installed locally. Project files and dependencies are deployed from your local computer to Azure. This results in a larger deployment package being uploaded to Azure. If for some reason, dependencies in your requirements.txt file can't be acquired by Core Tools, you must use the custom dependencies option for publishing.
456+
Using the `--build local` option, project dependencies are read from the requirements.txt file and those dependent packages are downloaded and installed locally. Project files and dependencies are deployed from your local computer to Azure. This results in a larger deployment package being uploaded to Azure. If for some reason, dependencies in your requirements.txt file can't be acquired by Core Tools, you must use the custom dependencies option for publishing.
457457

458458
### Custom dependencies
459459

@@ -463,7 +463,7 @@ If your project uses packages not publicly available to our tools, you can make
463463
pip install --target="<PROJECT_DIR>/.python_packages/lib/site-packages" -r requirements.txt
464464
```
465465

466-
When using custom dependencies, you should use the `--no-build` publishing option, since you have already installed the dependencies.
466+
When using custom dependencies, you should use the `--no-build` publishing option, since you have already installed the dependencies.
467467

468468
```command
469469
func azure functionapp publish <APP_NAME> --no-build
@@ -473,7 +473,7 @@ Remember to replace `<APP_NAME>` with the name of your function app in Azure.
473473

474474
## Unit Testing
475475

476-
Functions written in Python can be tested like other Python code using standard testing frameworks. For most bindings, it's possible to create a mock input object by creating an instance of an appropriate class from the `azure.functions` package. Since the [`azure.functions`](https://pypi.org/project/azure-functions/) package is not immediately available, be sure to install it via your `requirements.txt` file as described in the [package management](#package-management) section above.
476+
Functions written in Python can be tested like other Python code using standard testing frameworks. For most bindings, it's possible to create a mock input object by creating an instance of an appropriate class from the `azure.functions` package. Since the [`azure.functions`](https://pypi.org/project/azure-functions/) package is not immediately available, be sure to install it via your `requirements.txt` file as described in the [package management](#package-management) section above.
477477

478478
For example, following is a mock test of an HTTP triggered function:
479479

@@ -603,10 +603,10 @@ class TestFunction(unittest.TestCase):
603603
```
604604
## Temporary files
605605

606-
The `tempfile.gettempdir()` method returns a temporary folder, which on Linux is `/tmp`. Your application can use this directory to store temporary files generated and used by your functions during execution.
606+
The `tempfile.gettempdir()` method returns a temporary folder, which on Linux is `/tmp`. Your application can use this directory to store temporary files generated and used by your functions during execution.
607607

608608
> [!IMPORTANT]
609-
> Files written to the temporary directory aren't guaranteed to persist across invocations. During scale out, temporary files aren't shared between instances.
609+
> Files written to the temporary directory aren't guaranteed to persist across invocations. During scale out, temporary files aren't shared between instances.
610610
611611
The following example creates a named temporary file in the temporary directory (`/tmp`):
612612

@@ -617,22 +617,26 @@ import tempfile
617617
from os import listdir
618618

619619
#---
620-
tempFilePath = tempfile.gettempdir()
621-
fp = tempfile.NamedTemporaryFile()
622-
fp.write(b'Hello world!')
623-
filesDirListInTemp = listdir(tempFilePath)
624-
```
620+
tempFilePath = tempfile.gettempdir()
621+
fp = tempfile.NamedTemporaryFile()
622+
fp.write(b'Hello world!')
623+
filesDirListInTemp = listdir(tempFilePath)
624+
```
625625

626-
We recommend that you maintain your tests in a folder separate from the project folder. This keeps you from deploying test code with your app.
626+
We recommend that you maintain your tests in a folder separate from the project folder. This keeps you from deploying test code with your app.
627627

628628
## Cross-origin resource sharing
629629

630-
Azure Functions supports cross-origin resource sharing (CORS). CORS is configured [in the portal](functions-how-to-use-azure-function-app-settings.md#cors) and through the [Azure CLI](/cli/azure/functionapp/cors). The CORS allowed origins list applies at the function app level. With CORS enabled, responses include the `Access-Control-Allow-Origin` header. For more information, see [Cross-origin resource sharing](functions-how-to-use-azure-function-app-settings.md#cors).
630+
Azure Functions supports cross-origin resource sharing (CORS). CORS is configured [in the portal](functions-how-to-use-azure-function-app-settings.md#cors) and through the [Azure CLI](/cli/azure/functionapp/cors). The CORS allowed origins list applies at the function app level. With CORS enabled, responses include the `Access-Control-Allow-Origin` header. For more information, see [Cross-origin resource sharing](functions-how-to-use-azure-function-app-settings.md#cors).
631631

632632
CORS is fully supported for Python function apps.
633633

634634
## Known issues and FAQ
635635

636+
Thanks to your valuable feedbacks, we are able to maintain a list of troubleshooting guides for common issues:
637+
638+
* [ModuleNotFoundError and ImportError](functions-recover-module-not-found.md)
639+
636640
All known issues and feature requests are tracked using [GitHub issues](https://github.com/Azure/azure-functions-python-worker/issues) list. If you run into a problem and can't find the issue in GitHub, open a new issue and include a detailed description of the problem.
637641

638642
## Next steps

0 commit comments

Comments
 (0)