flask-testing icon indicating copy to clipboard operation
flask-testing copied to clipboard

cannot import name 'cached_property' from 'werkzeug'

Open bill-uncultured opened this issue 5 years ago • 31 comments

werkzeug just released 1.0.0, and the deprecated werkzeug.cached_property has been removed in favor of werkzeug.utils.cached_property.

This was addressed in October by pull request # 141, but it seems like it failed testing because the old python 3.3 and 2.6 couldn't be downloaded.

/usr/lib/python3.7/site-packages/flask_testing/__init__.py:13: in <module>
    from .utils import TestCase, LiveServerTestCase
/usr/lib/python3.7/site-packages/flask_testing/utils.py:35: in <module>
    from werkzeug import cached_property
E   ImportError: cannot import name 'cached_property' from 'werkzeug' (/usr/lib/python3.7/site-packages/werkzeug/__init__.py)

bill-uncultured avatar Feb 06 '20 22:02 bill-uncultured

Same issue as: https://github.com/noirbizarre/flask-restplus/issues/777

The proposed temporary solution is to pin Werkzeug==0.16.1 until a proper fix is put in place.

teapow avatar Feb 07 '20 09:02 teapow

I have created a fix for this https://github.com/jarus/flask-testing/pull/144

but CI infrastructure is not working

Downloading archive: https://storage.googleapis.com/travis-ci-language-archives/python/binaries/ubuntu/16.04/x86_64/python-2.6.tar.bz2 0.10s$ curl -sSf --retry 5 -o python-2.6.tar.bz2 ${archive_url} curl: (22) The requested URL returned error: 404 Not Found

erodrig avatar Feb 07 '20 15:02 erodrig

Another fix would be to edit the flask_testing/utils.py on line 35: Use from werkzeug.utils import cached_property instead of from werkzeug import cached_property

There's already a (closed) MR: https://github.com/jarus/flask-testing/pull/146

And an open one: https://github.com/jarus/flask-testing/pull/145

Edit: https://github.com/jarus/flask-testing/pull/145 has been merged

Ri1a avatar Feb 13 '20 14:02 Ri1a

Any updates regarding CI?

elgohr avatar Feb 19 '20 07:02 elgohr

As it is fixed. Could you release a new version? :)

Sispheor avatar Feb 27 '20 10:02 Sispheor

from werkzeug import cached_property

ImportError: cannot import name 'cached_property' from 'werkzeug' (C:\Users\BABA\PycharmProjects\Automation\venv\lib\site-packages\werkzeug_init_.py)

I am getting this error message

Rahulrao0786 avatar Mar 08 '20 09:03 Rahulrao0786

from flask import Flask from flask_restplus import Api, Resource from werkzeug.utils import *

app = Flask(name) api = Api(app)

@api.route('/language') class Language: def get(self): return {'hey': 'there'}

if name == "main": app.run(debug=True)

Rahulrao0786 avatar Mar 08 '20 09:03 Rahulrao0786

I've fixed this with: import werkzeug werkzeug.cached_property = werkzeug.utils.cached_property

vicmunoz avatar Mar 23 '20 16:03 vicmunoz

I've fixed this with: import werkzeug werkzeug.cached_property = werkzeug.utils.cached_property

Thanks! This works.

KareenaR avatar Mar 24 '20 00:03 KareenaR

fyi: this issue has been fixed in version 0.8.0

jnozsc avatar Apr 12 '20 03:04 jnozsc

Updating to Werkzeug==0.16.1 has fixed.

evertoncastro avatar Apr 15 '20 19:04 evertoncastro

Changed lib\site-packages\flask_restx\api.py", line 28, in from werkzeug import cached_property as below :

#from werkzeug import cached_property from werkzeug.utils import cached_property

mumapada avatar Jun 01 '20 14:06 mumapada

Still happening when upgrading to 1.0.1 version

felixminom avatar Aug 16 '20 19:08 felixminom

Still happening as of today.

philipperemy avatar Aug 25 '20 03:08 philipperemy

Still broken with werkzeug==1.0.1 as of 4th Sept 2020

Can be fixed by adding

import werkzeug
werkzeug.cached_property = werkzeug.utils.cached_property

before importing flask_restplus

SidJain1412 avatar Sep 04 '20 14:09 SidJain1412

Thanks @SidJain1412!

User-side workaround when not expecting to call werkzeug directly:

try:
    from flask_restplus import Resource, Api
except ImportError:
    import werkzeug
    werkzeug.cached_property = werkzeug.utils.cached_property
    from flask_restplus import Resource, Api

ctwardy avatar Dec 24 '20 13:12 ctwardy

This is still an issue. My API backend still fails when using untampered Flask due to "cannot import name 'cached_property' from 'werkzeug'".

JosephHubbard avatar Feb 02 '21 10:02 JosephHubbard

This is still an issue.

ontology_1 | File "./get_title_details.py", line 1, in ontology_1 | from robobrowser import RoboBrowser ontology_1 | File "/usr/local/lib/python3.6/site-packages/robobrowser/init.py", line 3, in ontology_1 | from .browser import RoboBrowser ontology_1 | File "/usr/local/lib/python3.6/site-packages/robobrowser/browser.py", line 8, in ontology_1 | from werkzeug import cached_property ontology_1 | ImportError: cannot import name 'cached_property'

bharath-kumarn avatar Apr 20 '21 07:04 bharath-kumarn

If I downgrade to Werkzeug==0.16.1 I get this other error: from werkzeug.local import ContextVar ImportError: cannot import name 'ContextVar' from 'werkzeug.local' (/home/paula/venvs/bidlab-venv/lib/python3.8/site-packages/werkzeug/local.py)

paulakeen avatar May 23 '21 21:05 paulakeen

Werkzeug==2.0.1 Flask==2.0.1 flask-restplus==0.13.0

With this requirements, the issue is still there, I downgraded to Werkzeug == 1.16.1 and downgraded other dependencies too... so it worked, but it should be working with latest version too, so please release a new version with this fix.

ritesh-hubbler avatar Jun 02 '21 07:06 ritesh-hubbler

Werkzeug==2.0.1 Flask==2.0.1 flask-restplus==0.13.0

With this requirements, the issue is still there, I downgraded to Werkzeug == 1.16.1 and downgraded other dependencies too... so it worked, but it should be working with latest version too, so please release a new version with this fix.

When I downgrade it says: flask 2.0.1 requires Werkzeug>=2.0, but you'll have werkzeug 0.16.1 which is incompatible.. Any suggestions?

rafael-andrade avatar Jun 07 '21 22:06 rafael-andrade

+1 Any ideas on how to resolve this? Haven't been able to solve the issue with the responses above and downgrading is not an option due to flask 2.0.1 not being compatible.

Flask==2.0.1 Flask-RESTful==0.3.9 Flask-Testing==0.7.1 Werkzeug==2.0.1

Traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/unittest/loader.py", line 434, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/local/lib/python3.7/unittest/loader.py", line 375, in _get_module_from_name
    __import__(name)
  File "/usr/src/app/project/tests/test_account_model.py", line 6, in <module>
    from project.tests.base import BaseTestCase
  File "/usr/src/app/project/tests/base.py", line 4, in <module>
    from flask_testing import TestCase
  File "/usr/local/lib/python3.7/site-packages/flask_testing/__init__.py", line 13, in <module>
    from .utils import TestCase, LiveServerTestCase
  File "/usr/local/lib/python3.7/site-packages/flask_testing/utils.py", line 35, in <module>
    from werkzeug import cached_property
ImportError: cannot import name 'cached_property' from 'werkzeug' (/usr/local/lib/python3.7/site-packages/werkzeug/__init__.py)

ipam73 avatar Jun 24 '21 03:06 ipam73

If I downgrade to Werkzeug==0.16.1 I get this other error: from werkzeug.local import ContextVar ImportError: cannot import name 'ContextVar' from 'werkzeug.local' (/home/paula/venvs/bidlab-venv/lib/python3.8/site-packages/werkzeug/local.py)

100%. I can't believe this is still going on.

toddman2 avatar Jul 13 '21 03:07 toddman2

Yes

On Tue, 13 Jul, 2021, 9:19 am toddman2, @.***> wrote:

If I downgrade to Werkzeug==0.16.1 I get this other error: from werkzeug.local import ContextVar ImportError: cannot import name 'ContextVar' from 'werkzeug.local' (/home/paula/venvs/bidlab-venv/lib/python3.8/site-packages/werkzeug/local.py)

100%. I can't believe this is still going on.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jarus/flask-testing/issues/143#issuecomment-878755563, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALR2PCSCNX3Y2TKMUKDSPDDTXOZWHANCNFSM4KRE6NPQ .

bharath-kumarn avatar Jul 13 '21 03:07 bharath-kumarn

Maybe related: Switch locals to be based on ContextVars #1778 https://github.com/pallets/werkzeug/pull/1778 pgjones commented on Mar 28, 2020 • ContextVar was introduced in Python 3.7 and is effectively the same as Local in Werkzeug (although a different API). They should work in Greenlet and threading contexts as before but also now in asyncio or other async/await contexts.

The storage attribute has been kept for backwards compatibility (even though it is a dunder it seems it is used, e.g. in tests).

The ident_func though must be deprecated as this is now handled by the ContextVar. This may cause some backwards incompatibility.

palmettobugz avatar Jul 19 '21 08:07 palmettobugz

Is there a solution to this problem? Because the first mention with Flask==2.0.1, flask-restplus==0.13.0, Werkzeug==2.0.1 was in June. This bug has yet to be resolved?

4vadim4 avatar Sep 16 '21 13:09 4vadim4

The issue still persists as of today... temporary workaround:

try: 
    from flask_restplus import Api, Resource
except ImportError:
    import werkzeug, flask.scaffold
    werkzeug.cached_property = werkzeug.utils.cached_property
    flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
    from flask_restplus import Api, Resource

Ninthpool avatar Jan 27 '22 18:01 Ninthpool

Current requirements.txt

Flask==2.1.2
Flask-Assets==2.0
flask-restplus==0.13.0
Werkzeug==2.1.2

and applied work around as (Downgrade is not possible due to compatibility)

try: 
    from flask_restplus import Api, Resource,Namespace
except ImportError:
    import werkzeug, flask.scaffold
    werkzeug.cached_property = werkzeug.utils.cached_property
    flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
    from flask_restplus import Api, Resource,Namespace
    
from models.hello_world import helloWork

data_edits_namespce=Namespace('data_edits')

data_edits_namespce.add_model('HelloWorld',helloWork)

Now the exception is coming

from flask_restplus import Api, Resource,Namespace
venv/lib/python3.10/site-packages/flask_restplus/model.py", line 8, in <module> from collections import OrderedDict, MutableMapping
ImportError: cannot import name 'MutableMapping' from 'collections' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/collections/__init__.py)

Not sure what i am missing here , does any one else also faced same issue

vikramadhav avatar Jun 13 '22 10:06 vikramadhav

We all should be migrating to flask-restx which is a wrapper for this library and it's been actively maintained.

felixminom avatar Jun 13 '22 17:06 felixminom

Clearly seems the owner is busy these days or no longer has an interest in fixing this. I'm cutting the guy some slack -- it's been a useful package so far. It would be appreciated if he could check in with us one way or another. Thanks for all your time you've put into so far.

toddman2 avatar Jun 14 '22 08:06 toddman2