Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No module named 'serverless_wsgi' on Azure #143

Open
MattiasDC opened this issue Jun 15, 2020 · 3 comments
Open

No module named 'serverless_wsgi' on Azure #143

MattiasDC opened this issue Jun 15, 2020 · 3 comments

Comments

@MattiasDC
Copy link

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

@logandk
Copy link
Owner

logandk commented Jun 16, 2020

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.

@MattiasDC
Copy link
Author

MattiasDC commented Jun 18, 2020

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.

@MattiasDC
Copy link
Author

MattiasDC commented Jun 20, 2020

Hi @logandk,

I created a fork and got it working on Azure. In a nutshell:

  • serverless_wsgi.py may be removed as azure provides a library to do this
  • Re-implement wsgi_handler.handler.

If you were to add support for Azure, feel free to use my changes.
Make sure to let me know in that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants