Flask: Greedy version requirement for werkzeug breaking fresh installs

Created on 6 Feb 2020  路  12Comments  路  Source: pallets/flask

Any fresh installs/updates to flask as of the last few hours crash with an import error

ImportError: cannot import name 'url_encode' from 'werkzeug' (/Users/me/env/lib/python3.7/site-packages/werkzeug/__init__.py)

due to a major version change in werkzeug (from 0.16.1 to 1.0.0), as per https://github.com/pallets/werkzeug/issues/1714

Most helpful comment

Setting werkzeug==0.16.1 in your requirements file fixes it. The issue is with the 1.0.0 version released yesterday https://pypi.org/project/Werkzeug/1.0.0/

All 12 comments

please show a full traceback.

Traceback (most recent call last):
  File "/Users/me/env/bin/airflow", line 26, in <module>
    from airflow.bin.cli import CLIFactory
  File "/Users/me/env/lib/python3.7/site-packages/airflow/bin/cli.py", line 70, in <module>
    from airflow.www.app import (cached_app, create_app)
  File "/Users/me/env/lib/python3.7/site-packages/airflow/www/app.py", line 26, in <module>
    from flask_wtf.csrf import CSRFProtect
  File "/Users/me/env/lib/python3.7/site-packages/flask_wtf/__init__.py", line 17, in <module>
    from .recaptcha import *
  File "/Users/me/env/lib/python3.7/site-packages/flask_wtf/recaptcha/__init__.py", line 2, in <module>
    from .fields import *
  File "/Users/me/env/lib/python3.7/site-packages/flask_wtf/recaptcha/fields.py", line 3, in <module>
    from . import widgets
  File "/Users/me/env/lib/python3.7/site-packages/flask_wtf/recaptcha/widgets.py", line 5, in <module>
    from werkzeug import url_encode
ImportError: cannot import name 'url_encode' from 'werkzeug' (/Users/me/env/lib/python3.7/site-packages/werkzeug/__init__.py)

Related stack trace for this same issue:

cannot import name 'cached_property': ImportError
Traceback (most recent call last):
  File "/var/task/handler.py", line 602, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 245, in lambda_handler
    handler = cls()
  File "/var/task/handler.py", line 139, in __init__
    self.app_module = importlib.import_module(self.settings.APP_MODULE)
  File "/var/lang/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/manage_consumer.py", line 35, in <module>
    from reclaim_consumer import create_app, DB
  File "/var/task/reclaim_consumer/__init__.py", line 13, in <module>
    from .api import API
  File "/var/task/reclaim_consumer/api.py", line 3, in <module>
    from flask_restplus import Api
  File "/var/task/flask_restplus/__init__.py", line 4, in <module>
    from . import fields, reqparse, apidoc, inputs, cors
  File "/var/task/flask_restplus/fields.py", line 17, in <module>
    from werkzeug import cached_property
ImportError: cannot import name 'cached_property'

Those are all flask extensions, not flask itself. Werkzeug 0.16 deprecated those top-level imports almost 5M ago, so the maintainers of those extensions had plenty of time to update them to use the correct imports...

flask-restplus actually looks dead: noirbizarre/flask-restplus#770

Is there a work around here? This has broken our ability to build our app which uses connexion. Explicitly setting 0.16.1 did not resolve this.

2020-02-06 22:32:49Traceback (most recent call last):
2020-02-06 22:32:49File "smid_service/app.py", line 1, in <module>
2020-02-06 22:32:49from smid_service import create_app
2020-02-06 22:32:49File "/app/smid_service/__init__.py", line 1, in <module>
2020-02-06 22:32:49import connexion
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/__init__.py", line 5, in <module>
2020-02-06 22:32:49from .apis import AbstractAPI # NOQA
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/apis/__init__.py", line 1, in <module>
2020-02-06 22:32:49from .abstract import AbstractAPI # NOQA
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 10, in <module>
2020-02-06 22:32:49from ..operations import make_operation
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/operations/__init__.py", line 1, in <module>
2020-02-06 22:32:49from .abstract import AbstractOperation # noqa
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/operations/abstract.py", line 10, in <module>
2020-02-06 22:32:49from ..decorators.response import ResponseValidator
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/decorators/response.py", line 12, in <module>
2020-02-06 22:32:49from .validation import ResponseBodyValidator
2020-02-06 22:32:49File "/usr/local/lib/python3.7/site-packages/connexion/decorators/validation.py", line 9, in <module>
2020-02-06 22:32:49from werkzeug import FileStorage
2020-02-06 22:32:49ImportError: cannot import name 'FileStorage' from 'werkzeug' (/usr/local/lib/python3.7/site-packages/werkzeug/__init__.py)

@tjohnson4 That error appears to be from SQLAlchemy.

@tjohnson4 That error appears to be from SQLAlchemy.

Correct traceback above

connexion is also not part of flask; so it's a problem on their side.

connexion is also not part of flask; so it's a problem on their side.

So they are to blame for this exception?
ImportError: cannot import name 'FileStorage' from 'werkzeug' (/usr/local/lib/python3.7/site-packages/werkzeug/__init__.py)

Yes, look at the traceback!

2020-02-06 22:32:49 File "/usr/local/lib/python3.7/site-packages/connexion/decorators/validation.py", line 9, in <module>
                                                                 ^^^^^^^^^
2020-02-06 22:32:49 from werkzeug import FileStorage

Setting werkzeug==0.16.1 in your requirements file fixes it. The issue is with the 1.0.0 version released yesterday https://pypi.org/project/Werkzeug/1.0.0/

I have learned in a hard way...
Thanks to @julenpardo, downgrading to werkzeug==0.16.1 resolves the issue, at least, for now.
Note that just installing Flask picks up the latest werkzeug, thus, werkzeug==0.16.1 must be explicitly stated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chuanconggao picture chuanconggao  路  4Comments

lnielsen picture lnielsen  路  3Comments

wadegilmer picture wadegilmer  路  4Comments

maangulo12 picture maangulo12  路  4Comments

davidhariri picture davidhariri  路  3Comments