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)
相关推荐
云程笔记2 小时前
004.环境搭建基础篇:Python、CUDA、cuDNN、PyTorch/TensorFlow安装与版本兼容性踩坑
pytorch·python·tensorflow
知行合一。。。7 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y7 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange7 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
GreenTea8 小时前
一文搞懂Harness Engineering与Meta-Harness
前端·人工智能·后端
pluvium278 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499998 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉8 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi8 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^8 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool