Skip to content

Commit d31a9e1

Browse files
author
Hanzhang Zeng (Roger)
committed
Add a trouble shooting page for module not found
1 parent 63f9729 commit d31a9e1

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: 'Troubleshoot error: Python Functions ModuleNotFoundError'
3+
description: Learn how to troubleshoot module not found error in Python function apps.
4+
author: Hazhzeng
5+
6+
ms.topic: article
7+
ms.date: 05/12/2020
8+
ms.author: hazeng
9+
---
10+
11+
# Troubleshoot error: "Python Functions module cannot be found"
12+
13+
This article helps you troubleshoot the following error string that appears in your Python function app:
14+
15+
> "Exception: ModuleNotFoundError: No module named 'module_name'."
16+
17+
This issue occurs when a Python function app fails to load Python module and here is a list of root causes:
18+
- [The package cannot be found](#the-package-cannot-be-found)
19+
- [The package is not resolved with proper Linux WHEEL](#the-package-is-not-resolved-with-proper-linux-wheel)
20+
- [The package is incompatible with the Python interpreter version](#the-package-is-incompatible-with-the-python-interpreter-version)
21+
- [The package conflicts with other packages](#the-package-conflicts-with-other-packages)
22+
- [The package only supports Windows or macOS platforms](#the-package-only-supports-windows-or-macos-platforms)
23+
24+
## Prerequisites
25+
26+
In order to identify the actual cause of your issue, we need to acquire the project files from your function app.
27+
- If the function app has `WEBSITE_RUN_FROM_PACKAGE` app setting and its value is a URL, please download the file by copy and paste the URL into your browser.
28+
- If the function app has `WEBSITE_RUN_FROM_PACKAGE` and it is set to `1`, please navigate to `https://<app-name>.scm.azurewebsites.net/api/vfs/data/SitePackages` and download the file from the latest `href` URL.
29+
- If the function app does not have the app setting mentioned above, please navigate to `https://<app-name>.scm.azurewebsites.net/api/settings` and find the URL under `SCM_RUN_FROM_PACKAGE`. Download the file by copy and paste the URL into your browser.
30+
- If none of the above steps works for you, please navigate to `https://<app-name>.scm.azurewebsites.net/DebugConsole` and reveal the content under `/home/site/wwwroot`.
31+
32+
The rest of this article helps you troubleshoot the following causes of this error by inspecting your function app's content, including how to identify and resolve each case.
33+
34+
## Diagnosis
35+
36+
### The package cannot be found
37+
38+
Go to `.python_packages/lib/python3.6/site-packages/<package-name>` or `.python_packages/lib/site-packages/<package-name>`. If the file path does not exist, your issue falls into this category.
39+
40+
Using third-party or outdated tools during deployment may cause this issue.
41+
42+
Please refer to [Enable Remote Build](#enable-remote-build) or [Build Native Dependencies](#build-native-dependencies) for mitigation.
43+
44+
### The package is not resolved with proper Linux WHEEL
45+
46+
Go to `.python_packages/lib/python3.6/site-packages/<package-name>-<version>-dist-info` or `.python_packages/lib/site-packages/<package-name>-<version>-dist-info`. Use your favorite text editor to open the **WHEEL** file and check the **Tag:** section. If the value of the tag does not contains **linux** in it, your issue falls into this category.
47+
48+
Since Python functions only runs on Linux (v2 on Debian Stretch, v3 on Debian Buster) in Azure, we expect the artifact contains Linux binaries. Using `--build local` flag in Core Tools, third-party, or outdated tools may be the cause of this issue.
49+
50+
Please refer to [Enable Remote Build](#enable-remote-build) or [Build Native Dependencies](#build-native-dependencies) for mitigation.
51+
52+
### The package is incompatible with the Python interpreter version
53+
54+
Go to `.python_packages/lib/python3.6/site-packages/<package-name>-<version>-dist-info` or `.python_packages/lib/site-packages/<package-name>-<version>-dist-info`. Use your favorite text editor to open the **METADATA** file and check the **Classifiers:** section. If the section does not contains **Python :: 3**, **Python :: 3.6**, **Python :: 3.7**, or **Python :: 3.8**, that means the package version is either too old, or most likely, the package is already out of maintenance.
55+
56+
You can check the Python version of your function app from **Azure Portal** -> **Function App** -> **Resource explorer (left panel)** -> **Click the 'Go' link**. Wait for a minute or two for the explorer to load up, and search **LinuxFxVersion** to reveal the Python version.
57+
58+
Please refer to [Update Package To The Latest Version](#update-package-to-the-latest-version) or [Replace Package With Equivalents](#replace-package-with-equivalents) for mitigation.
59+
60+
### The package conflicts with other packages
61+
62+
If you have ensured that the package is resolved correctly with the proper Linux WHEELs in it. Look up the documentation in `https://pypi.org/project/<package-name>/<package-version>`. In certain packages, the PyPi documentations may clarify the incompatible modules. For example in [azure 4.0.0](https://pypi.org/project/azure/4.0.0/), there's a statement as follow:
63+
64+
```text
65+
This package is not compatible with azure-storage.
66+
If you installed azure-storage, or if you installed azure 1.x/2.x and didn’t uninstall azure-storage,
67+
you must uninstall azure-storage first
68+
```
69+
70+
Please refer to [Update Package To The Latest Version](#update-package-to-the-latest-version) or [Replace Package With Equivalents](#replace-package-with-equivalents) for mitigation.
71+
72+
### The package only supports Windows or macOS platforms
73+
74+
Open the `requirements.txt` with your favorite text editor and check the package in `https://pypi.org/project/<package-name>`. Some packages only runs on Windows or macOS platforms (e.g. pywin32 only works on Windows).
75+
76+
The Module Not Found error may not surface when you are using Windows or macOS for local development. However, the package will fail to import on Azure Functions because we uses Linux at runtime. This is very likely caused by using `pip freeze` to export virtual environment into requirements.txt from your Windows or macOS machine during project initialization.
77+
78+
Please refer to [Replace Package With Equivalents](#replace-package-with-equivalents) or [Handcraft Requirements.txt](#handcraft-requirementstxt) for mitigation.
79+
80+
## Mitigations
81+
82+
### Enable Remote Build
83+
84+
If you are using [VSCode Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions), please ensure the latest version is installed. Check if `.vscode/settings.json` exist and has the setting `"azureFunctions.scmDoBuildDuringDeployment": true`. If not, please create the file with the `azureFunctions.scmDoBuildDuringDeployment` setting and try redeploying it.
85+
86+
If you are using [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools/releases), please ensure the latest version is installed. Go to your local function project folder, and use `func azure functionapp publish <app-name>` for deployment.
87+
88+
If you are manually publishing your package into the `https://<app-name>.scm.azurewebsites.net/api/zipdeploy` endpoint, please ensure the both **SCM_DO_BUILD_DURING_DEPLOYMENT** and **ENABLE_ORYX_BUILD** are set to **true**.
89+
90+
### Build Native Dependencies
91+
92+
Ensure the latest version of **docker** and [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools/releases) is installed. Go to your local function project folder, and use `func azure functionapp publish <app-name> --build-native-deps` for deployment.
93+
94+
### Update Package To The Latest Version
95+
96+
First, we can check the latest package version in `https://pypi.org/project/<package-name>` and check the **Classifiers:** section. The package should be **OS Independent**, or compatible with **POSIX** or **POSIX :: Linux** in Operating System. Also, the Programming Language should contains **Python :: 3**, **Python :: 3.6**, **Python :: 3.7**, or **Python :: 3.8**.
97+
98+
If all satisfied, you can update the package to the latest version by simply changing the line `<package-name>~=<latest-version>` in requirements.txt.
99+
100+
### Handcraft Requirements.txt
101+
102+
Some developers use `pip freeze > requirements.txt` to generate the list of Python packages for their developing environments, although this is handy and should work in most cases, in cross platform deployment scenarios (developing Azure Functions locally on Windows or macOS, but publishing the function app onto Linux), `pip freeze` will introduce **unexpected operating system specific dependencies**, or **dependencies for your local development environment**.
103+
104+
It will break Python function app Linux runtime. The best practice is to **check the import statement from each .py file** in your project source code, and **only check-in those modules in requirements.txt file**. This guarantees the resolution of packages can be handled properly on different operating systems.
105+
106+
### Replace Package With Equivalents
107+
108+
First, we should take a look into the latest version of the package in `https://pypi.org/project/<package-name>`. Usually, this package has their own GitHub page, go to the **Issues** section on GitHub and search if your issue has been fixed. If so, just simply update the package to the latest version.
109+
110+
Sometimes, the package may have been integrated into [Python Standard Library](https://docs.python.org/3/library/) (e.g. pathlib). If so, since we provide a certain Python distribution in Azure Functions (Python 3.6, Python 3.7, and Python 3.8), the package in your requirements.txt should be removed.
111+
112+
However, if you are facing an issue that it has not been fixed and you are on a very tight deadline. I encourage you to do some research and find a similar package for your project. Usually, the Python community will provide you with a wide variety of similar libraries that you can use.
113+
114+
## Next steps
115+
116+
> [!div class="nextstepaction"]
117+
> [Report Issues To Python Functions GitHub Repository](https://github.com/Azure/azure-functions-python-worker/issues)

0 commit comments

Comments
 (0)