I have created a simple Flask app and successfully deployed it to AWS Lambda. I am following the first steps of this tutorial.

When the Lambda is run, the following error appears in the log:

Unable to import module 'wsgi_handler': No module named 'werkzeug._compat'

I have installed serverless-python-requirements and serverless-wsgi. The folders are present in the zip folder that was uploaded to AWS.

I found the reference to werkzeug._compat in serverless_wsgi.py:

from werkzeug._compat import BytesIO, string_types, to_bytes, wsgi_encoding_dance

However, I cannot seem to find “_compat” anywhere in the Werkzeug folder where this function might be found. Am I missing a package or something?

Here is my serverless.yml:

# serverless.yml

service: serverless-flask

plugins:
  - serverless-python-requirements
  - serverless-wsgi

custom:
  wsgi:
    app: app.app
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux

provider:
  name: aws
  runtime: python3.6
  stage: dev
  region: us-east-1

functions:
  app:
    handler: wsgi_handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

And my requirements.txt:

click==8.0.0
dataclasses==0.8
Flask==2.0.0
itsdangerous==2.0.0
Jinja2==3.0.0
MarkupSafe==2.0.0
Werkzeug==2.0.0

Answer

Flask, Werkzeug and other pallets projects just had a major update, dropping python2 support and deleting _compat module. And AWS has’t resolve the capability issue yet.

The simplest fix will be downgrading Flask, Werkzeug, etc. to the previous major version.

Source: https://stackoverflow.com/q/67553537

172 5 5 0