環(huán)境:
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。將上一行改為下一行
# 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。將上一行改為下一行
# from flask.ext.cache import make_template_fragment_key
from flask_cache import make_template_fragment_key
3、安裝cachelib?
pip install?cachelib
【備注】werkzeug.contrib已經在1.0版本被移除了,所以無法從werkzeug.contrib.cache 中導入,需要單獨安裝cachelib文章來源:http://www.zghlxwxcb.cn/news/detail-700319.html
?4、修改文件/Users/zhangyanli/.pyenv/versions/flaskenv/lib/python3.7/site-packages/flask_cache/backends.py。將上一行改為下一行文章來源地址http://www.zghlxwxcb.cn/news/detail-700319.html
# from werkzeug.contrib.cache import (BaseCache, NullCache, SimpleCache, MemcachedCache,GAEMemcachedCache, FileSystemCache)
from cachelib import (BaseCache, NullCache, SimpleCache, MemcachedCache, FileSystemCache)
到了這里,關于flask-cache使用報錯Python3 ModuleNotFoundError: No module named ‘werkzeug.contrib‘的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!