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
+44-40Lines changed: 44 additions & 40 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
@@ -84,7 +84,7 @@ The main project folder (\_\_app\_\_) can contain the following files:
84
84
**.gitignore*: (Optional) declares files that are excluded from a git repo, such as local.settings.json.
85
85
**Dockerfile*: (Optional) used when publishing your project in a [custom container](functions-create-function-linux-custom-image.md).
86
86
87
-
Each function has its own code file and binding configuration file (function.json).
87
+
Each function has its own code file and binding configuration file (function.json).
88
88
89
89
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).
90
90
@@ -130,7 +130,7 @@ from __app__.shared_code import my_first_helper_function
130
130
131
131
## Triggers and Inputs
132
132
133
-
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.
133
+
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.
134
134
135
135
For example, the following code demonstrates the difference between the two:
136
136
@@ -257,12 +257,12 @@ To learn more about logging, see [Monitor Azure Functions](functions-monitoring.
257
257
258
258
## HTTP Trigger and bindings
259
259
260
-
The HTTP trigger is defined in the function.jon file. The `name` of the binding must match the named parameter in the function.
260
+
The HTTP trigger is defined in the function.jon file. The `name` of the binding must match the named parameter in the function.
261
261
In the previous examples, a binding name `req` is used. This parameter is an [HttpRequest] object, and an [HttpResponse] object is returned.
262
262
263
-
From the [HttpRequest] object, you can get request headers, query parameters, route parameters, and the message body.
263
+
From the [HttpRequest] object, you can get request headers, query parameters, route parameters, and the message body.
264
264
265
-
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).
265
+
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.
289
+
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.
290
290
291
291
Likewise, you can set the `status_code` and `headers` for the response message in the returned [HttpResponse] object.
292
292
@@ -324,13 +324,13 @@ def main():
324
324
325
325
### Use multiple language worker processes
326
326
327
-
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.
327
+
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.
328
328
329
-
The FUNCTIONS_WORKER_PROCESS_COUNT applies to each host that Functions creates when scaling out your application to meet demand.
329
+
The FUNCTIONS_WORKER_PROCESS_COUNT applies to each host that Functions creates when scaling out your application to meet demand.
330
330
331
331
## Context
332
332
333
-
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.
333
+
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:
347
347
348
-
`function_directory`
348
+
`function_directory`
349
349
The directory in which the function is running.
350
350
351
-
`function_name`
351
+
`function_name`
352
352
Name of the function.
353
353
354
-
`invocation_id`
354
+
`invocation_id`
355
355
ID of the current function invocation.
356
356
357
357
## Global variables
358
358
359
-
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.
359
+
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).
391
+
For local development, application settings are [maintained in the local.settings.json file](functions-run-local.md#local-settings-file).
392
392
393
-
## Python version
393
+
## Python version
394
394
395
395
Azure Functions supports the following Python versions:
396
396
@@ -401,13 +401,13 @@ Azure Functions supports the following Python versions:
401
401
402
402
<sup>*</sup>Official CPython distributions
403
403
404
-
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.
404
+
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.
405
405
406
-
When running locally, the runtime uses the available Python version.
406
+
When running locally, the runtime uses the available Python version.
407
407
408
408
## Package management
409
409
410
-
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`.
410
+
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`.
411
411
412
412
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.
424
+
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.
425
425
426
426
Project files and folders that are excluded from publishing, including the virtual environment folder, are listed in the .funcignore file.
427
427
428
428
There are three build actions supported for publishing your Python project to Azure:
429
429
430
-
+ 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.
431
-
+ Local build: Dependencies are obtained locally based on the contents of the requirements.txt file.
430
+
+ 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.
431
+
+ Local build: Dependencies are obtained locally based on the contents of the requirements.txt file.
432
432
+ Custom dependencies: Your project uses packages not publicly available to our tools. (Requires Docker.)
433
433
434
434
To build your dependencies and publish using a continuous delivery (CD) system, [use Azure Pipelines](functions-how-to-azure-devops.md).
435
435
436
436
### Remote build
437
437
438
-
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.
438
+
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.
439
439
440
440
```bash
441
441
func azure functionapp publish <APP_NAME>
442
442
```
443
443
444
444
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
445
445
446
-
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.
446
+
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.
447
447
448
448
### Local build
449
449
450
-
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.
450
+
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.
451
451
452
452
```command
453
453
func azure functionapp publish <APP_NAME> --build local
454
454
```
455
455
456
-
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
456
+
Remember to replace `<APP_NAME>` with the name of your function app in Azure.
457
457
458
-
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.
458
+
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.
459
459
460
460
### Custom dependencies
461
461
@@ -465,7 +465,7 @@ If your project uses packages not publicly available to our tools, you can make
@@ -475,7 +475,7 @@ Remember to replace `<APP_NAME>` with the name of your function app in Azure.
475
475
476
476
## Unit Testing
477
477
478
-
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.
478
+
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.
479
479
480
480
For example, following is a mock test of an HTTP triggered function:
481
481
@@ -605,10 +605,10 @@ class TestFunction(unittest.TestCase):
605
605
```
606
606
## Temporary files
607
607
608
-
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.
608
+
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.
609
609
610
610
> [!IMPORTANT]
611
-
> Files written to the temporary directory aren't guaranteed to persist across invocations. During scale out, temporary files aren't shared between instances.
611
+
> Files written to the temporary directory aren't guaranteed to persist across invocations. During scale out, temporary files aren't shared between instances.
612
612
613
613
The following example creates a named temporary file in the temporary directory (`/tmp`):
614
614
@@ -619,13 +619,13 @@ import tempfile
619
619
from os import listdir
620
620
621
621
#---
622
-
tempFilePath = tempfile.gettempdir()
623
-
fp = tempfile.NamedTemporaryFile()
624
-
fp.write(b'Hello world!')
625
-
filesDirListInTemp = listdir(tempFilePath)
626
-
```
622
+
tempFilePath = tempfile.gettempdir()
623
+
fp = tempfile.NamedTemporaryFile()
624
+
fp.write(b'Hello world!')
625
+
filesDirListInTemp = listdir(tempFilePath)
626
+
```
627
627
628
-
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.
628
+
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.
629
629
630
630
## Cross-origin resource sharing
631
631
@@ -635,6 +635,10 @@ CORS is fully supported for Python function apps.
635
635
636
636
## Known issues and FAQ
637
637
638
+
Thanks to your valuable feedback, we are able to maintain a list of troubleshooting guides for common issues:
639
+
640
+
*[ModuleNotFoundError and ImportError](recover-module-not-found.md)
641
+
638
642
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