azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

[BUG] ModuleNotFoundError: No module named 'dotenv'

Open emielsteerneman opened this issue 10 months ago • 6 comments

Investigative information

Please provide the following:
  • Timestamp: April 29th
  • Function App name: functionapp-test-dotenv
  • Function name(s) (as appropriate): main
  • Core Tools version: 4.0.5611

Repro steps

Step 1: Clone this repository, which is a project I have setup to demonstrate this bug. It is a python 3.11 function app in its most basic form, with dotenv as requirement. Step 2: Deploy this project to a Python 3.11 function app on Azure.

Expected behavior

The function app should deploy and start without errors. The /api/hello endpoint should work.

Actual behavior

An error ModuleError : No module named 'dotenv' is thrown.

Screenshot from 2024-04-30 11-34-50

Known workarounds

None

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions
python-dotenv

Related information

I've ran into this problem a month ago. I have been trying to deploy my actual project for some time now, but I keep running into errors that are not logged anywhere except in screenshot above. Neither the Core Tools, the Github CI/CD Actions, the Logstream, or the App Insights logs (specifically the traces) show any error when deploying. I've managed to trace it back to the dotenv module. Removing the dotenv module from the project makes it run without problem. Locally , using func start, everything works as intended. Only when deploying it to Azure does it fail.

  • Links to source Project that demonstrates this problem : https://github.com/emielsteerneman/functionapp_test_dotenv Related Stackoverflow post : https://stackoverflow.com/questions/78381457/where-to-find-azure-functionapp-deploy-errors-for-python-3-11-in-the-azure-porta/78382299 Actual project to deploy : https://github.com/emielsteerneman/TDP/tree/refactor Possibly related issue : https://github.com/Azure/azure-functions-python-worker/issues/1094
Source and stack trace
import azure.functions as func
import dotenv
from dotenv import load_dotenv

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route('hello')
def main(req):
    return func.HttpResponse("Hello World! : " + str(load_dotenv), mimetype="text/plain")

Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcException Worker failed to index functions Result: Failure Exception: ModuleNotFoundError: No module named 'dotenv'. Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound. Current sys.path: ['/tmp/functions\standby\wwwroot', '/home/site/wwwroot/.python_packages/lib/site-packages', '/azure-functions-host/workers/python/3.11/LINUX/X64', '/usr/local/lib/python311.zip', '/usr/local/lib/python3.11', '/usr/local/lib/python3.11/lib-dynload', '/usr/local/lib/python3.11/site-packages'] Stack: File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py', line 348, in _handle__functions_metadata_request fx_metadata_results = self.index_functions(function_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py', line 633, in index_functions indexed_functions = loader.index_function_app(function_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/utils/wrappers.py', line 49, in call raise extend_exception_message(e, message) File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/utils/wrappers.py', line 44, in call return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/loader.py', line 222, in index_function_app imported_module = importlib.import_module(module_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File '/usr/local/lib/python3.11/importlib/init.py', line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File '/home/site/wwwroot/function_app.py', line 2, in import dotenv

Error: No module named 'dotenv', Cannot find module. Error: No module named 'dotenv', Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound. Current sys.path: ['/tmp/functions\standby\wwwroot', '/home/site/wwwroot/.python_packages/lib/site-packages', '/azure-functions-host/workers/python/3.11/LINUX/X64', '/usr/local/lib/python311.zip', '/usr/local/lib/python3.11', '/usr/local/lib/python3.11/lib-dynload', '/usr/local/lib/python3.11/site-packages'] Traceback (most recent call last): File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/utils/wrappers.py', line 44, in call return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File '/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/loader.py', line 222, in index_function_app imported_module = importlib.import_module(module_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File '/usr/local/lib/python3.11/importlib/init.py', line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File '', line 1204, in _gcd_import File '', line 1176, in _find_and_load File '', line 1147, in _find_and_load_unlocked File '', line 690, in _load_unlocked File '', line 940, in exec_module File '', line 241, in _call_with_frames_removed File '/home/site/wwwroot/function_app.py', line 2, in import dotenv ModuleNotFoundError: No module named 'dotenv'

ERROR: Error: No module named 'dotenv', Cannot find module. ERROR: Error: No module named 'dotenv', Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound. Current sys.path: ['/tmp/functions\standby\wwwroot', '/home/site/wwwroot/.python_packages/lib/site-packages', '/azure-functions-host/workers/python/3.11/LINUX/X64', '/usr/local/lib/python311.zip', '/usr/local/lib/python3.11', '/usr/local/lib/python3.11/lib-dynload', '/usr/local/lib/python3.11/site-packages']

No job functions found. Try making your job classes and methods No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

emielsteerneman avatar Apr 30 '24 10:04 emielsteerneman

Thanks for reporting will check and update.

bhagyshricompany avatar May 03 '24 09:05 bhagyshricompany

@bhagyshricompany Any updates?

emielsteerneman avatar May 31 '24 23:05 emielsteerneman

Also having this issue!

nebriv avatar Jun 12 '24 12:06 nebriv

@gavin-aguiar please comment .Thanks

bhagyshricompany avatar Jul 02 '24 07:07 bhagyshricompany

@emielsteerneman are you still facing this issue? Few things to try out with your workflow file:

  • Remove the venv
  • Install the pip dependencies using this target --target=".python_packages/lib/site-packages

Here is a sample github workflow file. https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-github-actions?tabs=linux%2Cpython&pivots=method-manual#example-workflow-configuration-file

gavin-aguiar avatar Jul 08 '24 19:07 gavin-aguiar

@emielsteerneman are you still facing this issue? Few things to try out with your workflow file:

* Remove the venv

* Install the pip dependencies using this target `--target=".python_packages/lib/site-packages`

Here is a sample github workflow file. https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-github-actions?tabs=linux%2Cpython&pivots=method-manual#example-workflow-configuration-file

I don't know if I'm still facing this issue. For now I switched to Python 3.10 where this issue is not present. The Github workflow might fix the issue. Unfortunately, doing your steps is not possible with func azure functionapp publish. I don't have the time this week to test the Github workflow, but probably somewhere within the next three weeks.

emielsteerneman avatar Jul 14 '24 14:07 emielsteerneman