Azure function with python/FastAPI: sometimes fails due to missing a dependency: No module named 'nest_asyncio'.

member45232 51 Reputation points
2022-08-27T17:35:57.073+00:00

We use a function with Python 3.9 and FastApi, it works fine for our light traffic of a few thousand requests per day and only light data I/O. But in the past days we have seen short periods of <30 minutes where suddenly all/most incoming requests fail with a 500 server error and application insights showing the error: ModuleNotFoundError: No module named 'nest_asyncio'.

nest_asyncio is in our requirements file (managed by pipenv) and we haven't had problems with it the past months, neither is it a 100% failure rate. The function may "calm down" itself or a restart has helped until the next occurrence..

Any idea where this may come from? Could it be that one of the function containers is faulty and others not? It looks like the setup was not completed if nest_asyncio is not installed, or the virtual env is not active?

Full stacktrace:
Exception while executing function: Functions.app Result: Failure
Exception: ModuleNotFoundError: No module named 'nest_asyncio'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound
Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 355, in _handle__function_load_request
func = loader.load_function(
File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
raise extend_exception_message(e, message)
File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
return func(*args, **kwargs)
File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", line 127, in load_function
mod = importlib.import_module(fullmodname)
File "/usr/local/lib/python3.9/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/site/wwwroot/app/main.py", line 7, in <module>
import nest_asyncio

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2022-08-29T07:23:40.477+00:00

    @member45232 Thanks for reaching out. This looks like the know issue. Can you please confirm if you have python environment with host version 4.9.0 and up and have libraries like <<packagename>> in your requirements.txt may hit this error.
    Result: Failure Exception: ModuleNotFoundError: No module named 'packagename'. Please check the requirements.txt file for the missing module.

    **Mitigation: **
    To mitigate, the recommendation is to do any one of the following:

    az functionapp config set \
    -g <resource_group> \
    -n <function_app_name> \
    --subscription <subscription_id> \
    --linux-fx-version "DOCKER|mcr.microsoft.com/azure-functions/python:4.8.0-python3.9-appservice"

    The product team is working on fixing this issue. Let me know if the above helps you to resolve the issue.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.