flask-cache使用报错Python3 ModuleNotFoundError: No module named ‘werkzeug.contrib‘

环境:

Flask 2.1.2

Flask-Cache 0.13.1

Werkzeug 2.1.2

问题:

当使用了flask_cache时导致运行时问题出现:ModuleNotFoundError: No module named 'werkzeug.contrib'

解决方式如下:

1、修改文件/Users/zhangyanli/.pyenv/versions/flaskenv/lib/python3.7/site-packages/flask_cache/init.py。将上一行改为下一行

python 复制代码
# from werkzeug import import_string
from werkzeug.utils import import_string

【备注】这个修改是为了解决如下报错ImportError: cannot import name 'import_string' from 'werkzeug' (/Users/zhangyanli/.pyenv/versions/flaskenv/lib/python3.7/site-packages/werkzeug/init.py)

2、修改文件/Users/zhangyanli/.pyenv/versions/flaskenv/lib/python3.7/site-packages/flask_cache/jinja2ext.py。将上一行改为下一行

python 复制代码
# from flask.ext.cache import make_template_fragment_key
from flask_cache import make_template_fragment_key

3、安装cachelib

python 复制代码
pip install cachelib

【备注】werkzeug.contrib已经在1.0版本被移除了,所以无法从werkzeug.contrib.cache 中导入,需要单独安装cachelib

4、修改文件/Users/zhangyanli/.pyenv/versions/flaskenv/lib/python3.7/site-packages/flask_cache/backends.py。将上一行改为下一行

python 复制代码
# from werkzeug.contrib.cache import (BaseCache, NullCache, SimpleCache, MemcachedCache,GAEMemcachedCache, FileSystemCache)

from cachelib import (BaseCache, NullCache, SimpleCache, MemcachedCache, FileSystemCache)
相关推荐
费弗里1 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
ai小鬼头1 小时前
百度秒搭发布:无代码编程如何让普通人轻松打造AI应用?
前端·后端·github
考虑考虑1 小时前
@FilterRegistration和@ServletRegistration注解
spring boot·后端·spring
一只叫煤球的猫1 小时前
🔥 同事混用@Transactional和TransactionTemplate被我怼了,三种事务管理到底怎么选?
java·spring boot·后端
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
学Linux的语莫9 天前
python基础语法
开发语言·python
你的人类朋友9 天前
(●'◡'●)从Dockerfile快速入门Docker Compose
后端
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python