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
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-reference-python.md
+45-41Lines changed: 45 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
---
2
-
title: Python developer reference for Azure Functions
2
+
title: Python developer reference for Azure Functions
3
3
description: Understand how to develop functions with Python
4
4
ms.topic: article
5
5
ms.date: 12/13/2019
6
6
---
7
7
8
8
# Azure Functions Python developer guide
9
9
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).
11
11
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).
13
13
14
14
## Programming model
15
15
@@ -82,7 +82,7 @@ The main project folder (\_\_app\_\_) can contain the following files:
82
82
**.funcignore*: (Optional) declares files that shouldn't get published to Azure.
83
83
**.gitignore*: (Optional) declares files that are excluded from a git repo, such as local.settings.json.
84
84
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).
86
86
87
87
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).
88
88
@@ -128,7 +128,7 @@ from __app__.shared_code import my_first_helper_function
128
128
129
129
## Triggers and Inputs
130
130
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.
132
132
133
133
For example, the following code demonstrates the difference between the two:
134
134
@@ -255,12 +255,12 @@ To learn more about logging, see [Monitor Azure Functions](functions-monitoring.
255
255
256
256
## HTTP Trigger and bindings
257
257
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.
259
259
In the previous examples, a binding name `req` is used. This parameter is an [HttpRequest] object, and an [HttpResponse] object is returned.
260
260
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.
262
262
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).
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.
288
288
289
289
Likewise, you can set the `status_code` and `headers` for the response message in the returned [HttpResponse] object.
290
290
@@ -322,13 +322,13 @@ def main():
322
322
323
323
### Use multiple language worker processes
324
324
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.
326
326
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.
328
328
329
329
## Context
330
330
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.
The [**Context**](/python/api/azure-functions/azure.functions.context?view=azure-python) class has the following string attributes:
345
345
346
-
`function_directory`
346
+
`function_directory`
347
347
The directory in which the function is running.
348
348
349
-
`function_name`
349
+
`function_name`
350
350
Name of the function.
351
351
352
-
`invocation_id`
352
+
`invocation_id`
353
353
ID of the current function invocation.
354
354
355
355
## Global variables
356
356
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.
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).
390
390
391
-
## Python version
391
+
## Python version
392
392
393
393
Azure Functions supports the following Python versions:
394
394
@@ -399,13 +399,13 @@ Azure Functions supports the following Python versions:
399
399
400
400
<sup>*</sup>Official CPython distributions
401
401
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.
403
403
404
-
When running locally, the runtime uses the available Python version.
404
+
When running locally, the runtime uses the available Python version.
405
405
406
406
## Package management
407
407
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`.
409
409
410
410
For example, the following requirements file and pip command can be used to install the `requests` package from PyPI.
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.
423
423
424
424
Project files and folders that are excluded from publishing, including the virtual environment folder, are listed in the .funcignore file.
425
425
426
426
There are three build actions supported for publishing your Python project to Azure:
427
427
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.
430
430
+ Custom dependencies: Your project uses packages not publicly available to our tools. (Requires Docker.)
431
431
432
432
To build your dependencies and publish using a continuous delivery (CD) system, [use Azure Pipelines](functions-how-to-azure-devops.md).
433
433
434
434
### Remote build
435
435
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.
437
437
438
438
```bash
439
439
func azure functionapp publish <APP_NAME>
440
440
```
441
441
442
442
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
443
443
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.
445
445
446
446
### Local build
447
447
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.
449
449
450
450
```command
451
451
func azure functionapp publish <APP_NAME> --build local
452
452
```
453
453
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.
455
455
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.
457
457
458
458
### Custom dependencies
459
459
@@ -463,7 +463,7 @@ If your project uses packages not publicly available to our tools, you can make
@@ -473,7 +473,7 @@ Remember to replace `<APP_NAME>` with the name of your function app in Azure.
473
473
474
474
## Unit Testing
475
475
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.
477
477
478
478
For example, following is a mock test of an HTTP triggered function:
479
479
@@ -603,10 +603,10 @@ class TestFunction(unittest.TestCase):
603
603
```
604
604
## Temporary files
605
605
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.
607
607
608
608
> [!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.
610
610
611
611
The following example creates a named temporary file in the temporary directory (`/tmp`):
612
612
@@ -617,22 +617,26 @@ import tempfile
617
617
from os import listdir
618
618
619
619
#---
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
+
```
625
625
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.
627
627
628
628
## Cross-origin resource sharing
629
629
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).
631
631
632
632
CORS is fully supported for Python function apps.
633
633
634
634
## Known issues and FAQ
635
635
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
+
636
640
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.
0 commit comments