python中使用flask連線redis 出現 No module named 'flask.ext' 或No module named 'ext.cache'錯誤
前提: 安裝flask和flask-cache
示例: from flask import Flask from flask.ext.cache import Cache
cache = Cache()
config = { ‘CACHE_TYPE’: ‘redis’, ‘CACHE_REDIS_HOST’: ‘127.0.0.1’, ‘CACHE_REDIS_PORT’: 6379, ‘CACHE_REDIS_DB’: ‘’, ‘CACHE_REDIS_PASSWORD’: ‘’ }
app = Flask(name) app.config.from_object(config) cache.init_app(app)
@app.route(’/’) @cache.cached(timeout=60*2) def index(): name = ‘mink’ return name
if name == ‘main’: app.run()
按照上面的方式執行會出現error : No module named ‘flask.ext’
解決方案:
修改匯入方式:
將from flask.ext.cache import Cache 改為from flask_cache import Cache 再次執行.
然而會繼續遇到一個如下錯誤:
File “C:\Anaconda3-5.1.0-Windows-x86_64\lib\site-packages\flask_cache\
以上修改後執行正常!
- Serving Flask app “5555” (lazy loading)
- Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead.
- Debug mode: off