flask 2.0.1
一个小时前 flask 还正常,后来装了几个其他包,再启动就报错了
ImportError: cannot import name 'ContextVar' from 'werkzeug.local'
定位在
from werkzeug.local import ContextVar
可是 werkzeug/local.py 这个文件里确实没有 ContextVar
是哪里出了问题呢? pip 重装了也不行
1
killva4624 2021-06-07 19:49:55 +08:00
看看你的 import 的包路径有没有被修改过吧,可能是包升级过了:
import werkzeug |
2
killva4624 2021-06-07 19:50:08 +08:00
@killva4624 print(werkzeug.__file__)
|
3
zxCoder OP @killva4624 唉 迷惑 werkzeug 从 2.0.1 降到 2.0.0 就好了。。。。
|
4
killva4624 2021-06-07 20:03:18 +08:00 1
感觉是 contextvars 这个包的问题
local.py: ```python try: from contextvars import ContextVar if "gevent" in sys.modules or "eventlet" in sys.modules: # Both use greenlet, so first check it has patched # ContextVars, Greenlet <0.4.17 does not. import greenlet greenlet_patched = getattr(greenlet, "GREENLET_USE_CONTEXT_VARS", False) if not greenlet_patched: # If Gevent is used, check it has patched ContextVars, # <20.5 does not. try: from gevent.monkey import is_object_patched except ImportError: # Gevent isn't used, but Greenlet is and hasn't patched raise _CannotUseContextVar() else: if is_object_patched("threading", "local") and not is_object_patched( "contextvars", "ContextVar" ): raise _CannotUseContextVar() except (ImportError, _CannotUseContextVar): class ContextVar: # type: ignore """A fake ContextVar based on the previous greenlet/threading ident function. Used on Python 3.6, eventlet, and old versions of gevent. """ def __init__(self, _name: str) -> None: self.storage: t.Dict[int, t.Dict[str, t.Any]] = {} def get(self, default: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: return self.storage.get(_get_ident(), default) def set(self, value: t.Dict[str, t.Any]) -> None: self.storage[_get_ident()] = value ``` |