Skip to content

Module Not Found Issues

Hanzhang Zeng edited this page Jun 18, 2020 · 4 revisions

Official Link: https://docs.microsoft.com/en-us/azure/azure-functions/recover-module-not-found?tabs=vscode

There's a common issue when a new Python package is defined in requirements.txt, but after deployment, the Azure Functions keeps throwing Exception: ModuleNotFoundError: No module named '<module_name>'. There are a few things we can troubleshoot before creating a GitHub issue in this repository.

Prerequisites

Acquire your function app content

  1. In your app setting, there may be a WEBSITE_RUN_FROM_PACKAGE which contains a URL. Try download the content by putting this URL into your browser, you should be able to download your own function app content from it. Use 7-Zip to open the downloaded package and continue on the Diagnosis section.
  2. If the Step 1 doesn't work for you, or the WEBSITE_RUN_FROM_PACKAGE is missing, landing on https://function_app_name.scm.azurewebsites.net/api/settings page and there should be an SCM_RUN_FROM_PACKAGE setting containing a URL. Try download the content by putting this URL into your browser, you should be able to download your own function app content from it. Use 7-Zip to open the downloaded package and continue on Diagnosis section.
  3. If the Step 1 or Step 2 doesn't work for you, go to https://function_app_name.scm.azurewebsites.net/DebugConsole and it should spin up a debug console. Use cd /home/site/wwwroot and do an ls in this folder. If you see your function app content, continue on Diagnosis section. Otherwise, please raise an issue.

Diagnosis

Package cannot be found

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. Using third-party or outdated tools may be the cause of this issue. Please refer to Enable remote build or Build native dependencies for mitigation.

Package is not resolved with Linux WHEEL

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. 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 or outdated tools may be the cause of this issue. Please refer to Enable remote build or Build native dependencies for mitigation.

Package is incompatible with the Python interpreter version

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 Classifier: 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 too old, or most likely, the package is already out of maintenance. Please refer to Update package to the latest version for mitigation.

Package is incompatible with each others

If you ensure that the broken package is resolved correctly with Linux WHEELs in it. Look up the documentation in https://pypi.org/project/package_name/package_version. In some packages, there are statements for incompatible modules. For example in azure==4.0.0, there's a statement as follow:

This package is not compatible with azure-storage.
If you installed azure-storage, or if you installed azure 1.x/2.x and didn’t uninstall azure-storage,
you must uninstall azure-storage first

Please refer to Replace package with equivalents for mitigation.

Package only supports Windows/MacOS platform

Some packages only runs on Windows operating system (e.g. pywin32 only runs on Windows). The Module Not Found error may not surface when you are using Windows/MacOS for local development. However, the package will fail to import on Azure Functions because we are using Linux (v2 on Debian Stretch, v3 on Debian Buster) at runtime. This most likely occurs when you use pip freeze to export your virtual environment into requirements.txt from your Windows/MacOS machine. Please refer to Replace package with equivalents or Handcraft requirements.txt for mitigation.

Mitigations

Enable remote build:

If you are using VSCode, 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.

If you are using Azure Functions Core Tools, please ensure the latest version is installed. Go to your local function project folder, and use func azure functionapp publish <function_app_name> for deployment.

Build native dependencies:

Ensure the latest version of docker and Azure Functions Core Tools is installed. Go to your local function project folder, and use func azure functionapp publish <function_app_name> --build-native-deps for deployment.

Update package to the latest version

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. If all satisfied, you can update the package to the latest version by changing the line <package_name>~=<latest_version> in requirements.txt.

Replace package with equivalents

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.

Sometimes, the package may have been integrated into Python as a built-in library. You can check a list of Python Standard Libraries from here. If so, the package in your requirements.txt should be removed.

However, if you are facing an issue that it hasn't 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, you should have a wide variety of choices for the libraries you can use.

Handcraft requirements.txt

Some developers use pip freeze > requirements.txt to generate the list of Python packages, although this should work in most cases, sometimes it doesn't. In cross platform deployment scenarios (developing Azure Functions locally on Windows/MacOS but publishing the function app onto Linux), pip freeze will introduce some OS specific libraries due to the dependencies of the package (e.g. pywin32). This will break our Linux runtime and result in a Module Not Found error. The best practice is to check the import statement in the .py files in your project source code and only list those modules in requirements.txt file. This guarantees the resolution of packages can be handle properly on different operating systems.