theskumar / python-dotenv

Reads key-value pairs from a .env file and can set them as environment variables. It helps in developing applications following the 12-factor principles.
https://saurabh-kumar.com/python-dotenv/
BSD 3-Clause "New" or "Revised" License
7.66k stars 430 forks source link

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

Open funatsufumiya opened 2 years ago

funatsufumiya commented 2 years ago

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__'])
af-jka commented 2 years ago

Same problem here, since last week

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

somatusds commented 2 years ago

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)

KantiCodes commented 2 years ago

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

polavishnu4444 commented 2 years ago

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).

dmarx commented 2 years ago

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