python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

ModuleNotFoundError: No module named '__main__' / load_dotenv() error on the latest Azure Functions (Python 3.9, Linux)

Open funatsufumiya opened this issue 3 years ago • 5 comments

dotenv.load_dotenv() caused ModuleNotFoundError: No module named '__main__' on the latest Azure Functions Python 3.9 environment (used as HTTP Trigger).

This is happening when use find_dotenv() internally, so this can be fixed by specifying env path like below.

envpath = os.path.join(os.path.dirname(__file__), '.env')
dotenv.load_dotenv(envpath)

(Issue #299 is maybe similar.)

Error logs:

Result: Failure Exception: ModuleNotFoundError: No module named '__main__' Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 448, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 691, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/HttpTrigger_jetson_01_test/__init__.py", line 43, in main container_client = adls.get_container_client(place_name) File "/home/site/wwwroot/adls.py", line 14, in get_container_client dotenv.load_dotenv() File "/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 322, in load_dotenv dotenv_path = find_dotenv() File "/home/site/wwwroot/.python_packages/lib/site-packages/dotenv/main.py", line 275, 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 272, in _is_interactive main = __import__('__main__', None, None, fromlist=['__file__'])

funatsufumiya avatar Aug 23 '22 09:08 funatsufumiya

Same problem here, since last week

[EDIT] Quick workaround if applicapable: Switch to function runtime version ~3

af-jka avatar Aug 24 '22 12:08 af-jka

Also recently began running into No module named '__main__' errors in previously functional HTTP Trigger Python Azure Functions, specifically when calling functions in __init__.py imported from another location:

Scrubbed example:

from src import file1

...

file1.func1(arg1, arg2)

file1 imports fine, then when func1 is called receive the error: ModuleNotFoundError: No module named '__main__'

Per @afjankahnt was able to work around by setting Function Runtime Version from ~4 to ~3 -- now works successfully (as before)

somatusds avatar Aug 25 '22 15:08 somatusds

Same problem here, since last week

[EDIT] Quick workaround if applicapable: Switch to function runtime version ~3

Thank you @afjankahnt , your solution worked for me worked for me.

In case someone wants to avoid 1 google search I am pinning how to switch runtime version:

https://docs.microsoft.com/en-us/azure/azure-functions/set-runtime-version?tabs=portal#view-and-update-the-current-runtime-version

KantiCodes avatar Aug 29 '22 15:08 KantiCodes

Support for runtime ~3 is going to end by Dec 2022. Its not far away, this issue would be good to be fixed.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=azure-cli%2Cin-process%2Cv4&pivots=programming-language-python (important note at the top mentions support end).

polavishnu4444 avatar Sep 01 '22 09:09 polavishnu4444

looks like the issue is probably on azure's side, and they're working on it: https://github.com/Azure/azure-functions-python-worker/issues/1094

dmarx avatar Sep 08 '22 02:09 dmarx