serverless-wsgi
serverless-wsgi copied to clipboard
No module named 'serverless_wsgi' on Azure
I get the following error when using serverless-wsgi on Azure.
Exception while executing function: Functions.app Result: Failure Exception: ModuleNotFoundError: No module named 'serverless_wsgi' Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 238, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 66, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.8/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/wsgi_handler.py", line 22, in
import serverless_wsgi
When reading up on the python developer reference here, you can see that referring to a local module only works with explicit relative or absolute paths, but not implicit relative.
A fix might be to change line 22 of wsgi_handler.py to
from . import serverless_wsgi
I'm using serverless_wsgi version 1.7.5.
Note that running serverless locally with 'sls wsgi serve' works like a charm :)
Hi @MattiasDC,
Yeah, I've not tested on Azure personally - on AWS, the package root is PYTHONPATH
, so it's not importing from a relative path and there's no issue.
Did you test that from . import serverless_wsgi
works on Azure? And I assume you had to add __init__.py
to the package root as well?
In that case, I suppose we catch the exception when importing from PYTHONPATH
and fall back to the relative import. Another option would be to check whether PYTHONPATH
is where wsgi_handler.py
is and whether there's an __init__.py
(assuming it's required), and import accordingly. I don't think we should add __init__.py
automatically, but we should probably emit a warning if it's required on Azure and is not present in the project.
Hi,
Putting the following code before the import fixes that problem (and the importing of the module containing the app).
root = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, root)
There seem to be some other issues though, it seems Azure expects other function parameters. An example Azure Function with WSGI can be found here.