Skip to content

Commit 17b17a7

Browse files
authored
Merge pull request #115074 from Hazhzeng/hazeng/module-not-found
Add Azure Functions Module Not Found Troubleshooting Guide
2 parents f1c6d2e + 78af6ce commit 17b17a7

File tree

4 files changed

+185
-41
lines changed

4 files changed

+185
-41
lines changed

articles/azure-functions/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
- name: Machine learning with TensorFlow
5050
href: functions-machine-learning-tensorflow.md
5151
- name: Image classification with PyTorch
52-
href: machine-learning-pytorch.md
52+
href: machine-learning-pytorch.md
5353
- name: Create a custom Linux image
5454
href: functions-create-function-linux-custom-image.md
5555
- name: Functions on IoT Edge device
@@ -335,6 +335,8 @@
335335
items:
336336
- name: Troubleshoot storage
337337
href: functions-recover-storage-account.md
338+
- name: Troubleshoot Python dependencies
339+
href: recover-module-not-found.md
338340
- name: Reference
339341
items:
340342
- name: API references

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

Lines changed: 44 additions & 40 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

@@ -84,7 +84,7 @@ The main project folder (\_\_app\_\_) can contain the following files:
8484
* *.gitignore*: (Optional) declares files that are excluded from a git repo, such as local.settings.json.
8585
* *Dockerfile*: (Optional) used when publishing your project in a [custom container](functions-create-function-linux-custom-image.md).
8686

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

8989
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).
9090

@@ -130,7 +130,7 @@ from __app__.shared_code import my_first_helper_function
130130

131131
## Triggers and Inputs
132132

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

135135
For example, the following code demonstrates the difference between the two:
136136

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

258258
## HTTP Trigger and bindings
259259

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.
261261
In the previous examples, a binding name `req` is used. This parameter is an [HttpRequest] object, and an [HttpResponse] object is returned.
262262

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

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

267267
```python
268268
def main(req: func.HttpRequest) -> func.HttpResponse:
@@ -276,7 +276,7 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
276276
pass
277277
else:
278278
name = req_body.get('name')
279-
279+
280280
if name:
281281
return func.HttpResponse(f"Hello {name}!", headers=headers)
282282
else:
@@ -286,7 +286,7 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
286286
)
287287
```
288288

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

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

@@ -324,13 +324,13 @@ def main():
324324

325325
### Use multiple language worker processes
326326

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

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

331331
## Context
332332

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

335335
For example:
336336

@@ -345,18 +345,18 @@ def main(req: azure.functions.HttpRequest,
345345

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

348-
`function_directory`
348+
`function_directory`
349349
The directory in which the function is running.
350350

351-
`function_name`
351+
`function_name`
352352
Name of the function.
353353

354-
`invocation_id`
354+
`invocation_id`
355355
ID of the current function invocation.
356356

357357
## Global variables
358358

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

361361
```python
362362
CACHED_DATA = None
@@ -388,9 +388,9 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
388388
logging.info(f'My app setting value:{my_app_setting_value}')
389389
```
390390

391-
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).
392392

393-
## Python version
393+
## Python version
394394

395395
Azure Functions supports the following Python versions:
396396

@@ -401,13 +401,13 @@ Azure Functions supports the following Python versions:
401401

402402
<sup>*</sup>Official CPython distributions
403403

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

406-
When running locally, the runtime uses the available Python version.
406+
When running locally, the runtime uses the available Python version.
407407

408408
## Package management
409409

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`.
411411

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

@@ -421,41 +421,41 @@ pip install -r requirements.txt
421421

422422
## Publishing to Azure
423423

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

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

428428
There are three build actions supported for publishing your Python project to Azure:
429429

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.
432432
+ Custom dependencies: Your project uses packages not publicly available to our tools. (Requires Docker.)
433433

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

436436
### Remote build
437437

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

440440
```bash
441441
func azure functionapp publish <APP_NAME>
442442
```
443443

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

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

448448
### Local build
449449

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

452452
```command
453453
func azure functionapp publish <APP_NAME> --build local
454454
```
455455

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

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

460460
### Custom dependencies
461461

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

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

470470
```command
471471
func azure functionapp publish <APP_NAME> --no-build
@@ -475,7 +475,7 @@ Remember to replace `<APP_NAME>` with the name of your function app in Azure.
475475

476476
## Unit Testing
477477

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

480480
For example, following is a mock test of an HTTP triggered function:
481481

@@ -605,10 +605,10 @@ class TestFunction(unittest.TestCase):
605605
```
606606
## Temporary files
607607

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

610610
> [!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.
612612
613613
The following example creates a named temporary file in the temporary directory (`/tmp`):
614614

@@ -619,13 +619,13 @@ import tempfile
619619
from os import listdir
620620

621621
#---
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+
```
627627

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

630630
## Cross-origin resource sharing
631631

@@ -635,6 +635,10 @@ CORS is fully supported for Python function apps.
635635

636636
## Known issues and FAQ
637637

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+
638642
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.
639643

640644
## Next steps
52.8 KB
Loading

0 commit comments

Comments
 (0)