Python Azure Function: Failure Exception: ModuleNotFoundError: No module named '__main__'

Viewed 1701

I'm triggering an Azure Function that fetches some data and writes it to a SQL db. The file Function works perfectly locally, but when I deploy it to Azure I keep getting the following error:

Result: Failure Exception: ModuleNotFoundError: No module named '__main__'. Troubleshooting 
Guide: https://aka.ms/functions-modulenotfound Stack: File "/azure-functions- 
host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 301, 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 83, 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/jobinfo/__init__.py", line 7, in <module> from .auth_and_get import * File 
"/home/site/wwwroot/jobinfo/auth_and_get.py", line 9, in <module> load_dotenv() File 
"/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 317, in 
load_dotenv f = dotenv_path or stream or find_dotenv() File 
"/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 265, in 
find_dotenv if usecwd or _is_interactive() or getattr(sys, 'frozen', False): File 
"/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 262, in 
 _is_interactive main = __import__('__main__', None, None, fromlist=['__file__'])

In my Function file __init__.py I import a module auth_and_get.py where I've made methods, which I wanna call inside my Azure Function, and I think that something's wrong with my import, but I've tried fixing it without luck.

My current __init__.py file is

from __future__ import absolute_import

import datetime
import logging

import azure.functions as func
from .auth_and_get import *


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    authenticate()
    users()
    to_db()

    logging.info('Python timer trigger function ran at %s', utc_timestamp)

and I suspect that it's the following statment that fails: from .auth_and_get import *. I've tried removing the dot such that from auth_and_get import *, but then the module becomes unresolveable.

The structure is

ProjectFolder/
| - .venv
| - .vscode
|jobinfo/
| | - __pycache__
| | - __init__.py
| | - auth_and_get.py
| | - function.json
| | - sample.dat
| - .funcignore
| - host.json
| - local.settings.json
| - proxies.json
| - requirements.txt

QUESTION

  • Why do I get the Failure Exception: ModuleNotFoundError: No module named '__main__' error?
2 Answers

So after a variety of attempts, I found a solution by adding

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__))))

to the __init__.py file.

Have you set startup command on Azure Portal for your App Service? I mean Settings->Configuration->General Settings and then in the field "Startup Command" you should have something like:

gunicorn --bind=0.0.0.0 --timeout 600 --chdir jobinfo __init__:<app object>

Of course you can set another command (like non-Gunicorn), but it is an example based on Azure docs.

Azure by default looks for main module named app.py or application.py.

It's explained here: https://docs.microsoft.com/en-us/azure/app-service/configure-language-python#customize-startup-command

ModuleNotFoundError: No module named '__main__' means that Azure looks for module named __main__ instead of __init__.

Related