Skip to content

Can't import WsgiMiddleware #52

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

Closed
cecilphillip opened this issue Mar 19, 2020 · 13 comments · Fixed by #54
Closed

Can't import WsgiMiddleware #52

cecilphillip opened this issue Mar 19, 2020 · 13 comments · Fixed by #54

Comments

@cecilphillip
Copy link

cecilphillip commented Mar 19, 2020

I just created a new functions project and tried to import WsgiMiddleware as mentioned in #45, but it won't resolve for some reason.

I'm using Python 3.7.4, core tools version 3.0.2245, and v1.2.0 of the azure-functions package

from azure.functions import (
    HttpRequest,
    HttpRequest,
    WsgiMiddleware,
    Context
)

def web_application(env, start_response, ):
    status = '200 OK'
    headers = [('Content-type', 'text/html')]
    start_response(status, headers)

    return[b'<strong>Raw WSGI application!!<strong>']


def main(req: HttpRequest,  context: Context) -> HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    return WsgiMiddleware(web_application).main(req, context)

Running the code above results in

[3/19/2020 8:10:25 AM]  INFO: Starting Azure Functions Python Worker.
[3/19/2020 8:10:25 AM]  INFO: Worker ID: 06c1081b-5d78-4298-9895-3171d3d6cb21, Request ID: e3580ac0-97a4-4504-b69c-674bdc108fc0, Host Address: 127.0.0.1:58404
[3/19/2020 8:10:25 AM]  INFO: Successfully opened gRPC channel to 127.0.0.1:58404
[3/19/2020 8:10:25 AM]  INFO: Received WorkerInitRequest, request ID e3580ac0-97a4-4504-b69c-674bdc108fc0
[3/19/2020 8:10:25 AM]  INFO: Received FunctionLoadRequest, request ID: e3580ac0-97a4-4504-b69c-674bdc108fc0, function ID: 09177bec-68a3-464d-8fef-59a47aa70125
[3/19/2020 8:10:25 AM]  INFO: Successfully processed FunctionLoadRequest, request ID: e3580ac0-97a4-4504-b69c-674bdc108fc0, function ID: 09177bec-68a3-464d-8fef-59a47aa70125
[3/19/2020 8:10:25 AM]  INFO: Received FunctionLoadRequest, request ID: e3580ac0-97a4-4504-b69c-674bdc108fc0, function ID: fa8828e4-d216-4ebc-9794-cbbcd9b70fed
[3/19/2020 8:10:25 AM] Worker failed to function id fa8828e4-d216-4ebc-9794-cbbcd9b70fed.
[3/19/2020 8:10:25 AM] Result: Failure
[3/19/2020 8:10:25 AM] Exception: ImportError: cannot import name 'WsgiMiddleware' from 'azure.functions' (/usr/local/Cellar/azure-functions-core-tools@3/3.0.2245/workers/python/3.7/OSX/X64/azure/functions/__init__.py)
[3/19/2020 8:10:25 AM] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2245/workers/python/3.7/OSX/X64/azure_functions_worker/dispatcher.py", line 242, in _handle__function_load_request
[3/19/2020 8:10:25 AM]     func_request.metadata.entry_point)
[3/19/2020 8:10:25 AM]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2245/workers/python/3.7/OSX/X64/azure_functions_worker/loader.py", line 66, in load_function
[3/19/2020 8:10:25 AM]     mod = importlib.import_module(fullmodname)
[3/19/2020 8:10:25 AM]   File "/Users/cecilphillip/.pyenv/versions/3.7.4/lib/python3.7/importlib/__init__.py", line 127, in import_module
[3/19/2020 8:10:25 AM]     return _bootstrap._gcd_import(name[level:], package, level)
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap_external>", line 728, in exec_module
[3/19/2020 8:10:25 AM]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[3/19/2020 8:10:25 AM]   File "/Users/cecilphillip/Desktop/toss/WsgiApi/__init__.py", line 1, in <module>
[3/19/2020 8:10:25 AM]     from azure.functions import (
[3/19/2020 8:10:25 AM] .
[3/19/2020 8:10:25 AM] Host restarted.
[3/19/2020 8:10:25 AM] Stopping 

@Hazhzeng am I missing something?

@Hazhzeng
Copy link
Contributor

Hazhzeng commented Mar 19, 2020

Hi @cecilphillip,

  1. The azure functions core tools does not have the latest version of azure-functions library yet. If you want to test out this feature, please remove the azure folder in your core tools (rm -rf azure-functions-core-tools\bin\workers\python\3.8\OSX\X64\azure) and install azure-functions~=1.2.0 in your Python virtual environment.

  2. In your code, return WsgiMiddleware(web_application).main(req, context) should be return WsgiMiddleware(web_application).handle(req, context) as mentioned here

  3. Another nit is in your third line, HttpRequest should be HttpResponse.

Thanks for reporting this issue.

@cecilphillip
Copy link
Author

@Hazhzeng Thanks! I just assumed that it would use the version of the package that's specified in the requirements.txt file.

So does the core tools just ignore that package and install other dependencies? I'm just trying to understand how that works.

@Hazhzeng
Copy link
Contributor

Hi @cecilphillip

Yes, so far we ignore the azure-functions library in requirements.txt in local dev and host runtime, and we bound a specific version of azure-functions library in every host release.

@cecilphillip
Copy link
Author

@Hazhzeng So how do devs consuming the library know what version of the they can depend on?

@Hazhzeng
Copy link
Contributor

@cecilphillip we have a schedule for rolling out new library every 2 weeks.

We have a discussion with Maheer (who was the former developer on Python worker), if we bind the library version with customer's code, the customer needs to redeploy their function app when we release a new version of azure-functions library. These releases are backward compatible and include bug fixes on python worker and azure-functions library.

We made a decision to roll-out these libraries with the host so the customer's app can be run on the latest version automatically without manual intervention. If you have any doubts, feel free to let me know and suggestions are welcomed.

cc: @anirudhgarg

Thanks,

@cecilphillip
Copy link
Author

cecilphillip commented Mar 20, 2020

@Hazhzeng

I think most devs would have the expectation that whatever package versions they install should be the versions that ends up in their deployment, but I also understand your reasoning. I have 2 suggestions

  • We should document that behavior around azure-functions in the official docs. Maybe somewhere in this article
  • When the function runtime loads up locally, would it be possible to output the version of azure-functions that's getting loaded, or maybe have a static list somewhere that shows the mappings between the azure-functions versions and host runtime versions?

@cecilphillip
Copy link
Author

@Hazhzeng is 1.2.0 rolled out to functions-core-tools yet?

@tvadim
Copy link

tvadim commented Apr 23, 2020

@Hazhzeng would you mind re-opening the issue as I cannot see a way it's resolved: for a local installation I can obviously remove the azure folder in the core tools directory. But how would I do that when my function is deployed to Azure?

Exception while executing function: Functions.impl Result: Failure
Exception: AttributeError: module 'azure.functions' has no attribute 'WsgiMiddleware'
Stack:   File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 242, in _handle__function_load_request
    func_request.metadata.entry_point)
  File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/loader.py", line 66, in load_function
    mod = importlib.import_module(fullmodname)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/site/wwwroot/impl/main.py", line 124, in <module>
    main = func.WsgiMiddleware(my_skill).main

@Hazhzeng
Copy link
Contributor

Oh sorry, I think the merge of the PR automatically closed this issue, I will reopen it again.

@Hazhzeng Hazhzeng reopened this Apr 23, 2020
@cecilphillip cecilphillip changed the title Can't important WsgiMiddleware Can't import WsgiMiddleware Apr 26, 2020
@Hazhzeng Hazhzeng self-assigned this Apr 29, 2020
@yubozhao
Copy link

yubozhao commented Jun 11, 2020

I ran into this issue today. Any update on this?

@tvadim did you find any workaround?

I looked into the Dockerfile that was provided for Azure function. It has 1.0.8 installed. I opened an issue about it over there. Azure/azure-functions-docker#281

My current workaround is created a custom Dockerfile that manually remove it and then install the latest one with pip

@Hazhzeng
Copy link
Contributor

Hazhzeng commented Jul 1, 2020

This one is already deployed on production. Closing the thread.

@Hazhzeng Hazhzeng closed this as completed Jul 1, 2020
@yubozhao
Copy link

yubozhao commented Jul 1, 2020

@Hazhzeng Does this mean that we no longer need to base the image from mcr.microsoft.com/azure-functions/python:3.0.13901-python3.7?

@anirudhgarg
Copy link
Member

Yes now this is included in the Consumption plan and in the images we use in the Dedicated/Premium plan and in CoreTools.

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

Successfully merging a pull request may close this issue.

5 participants