python - Flask使用 Blueprint 出现ImportError: cannot import name ’Blueprint’?
问题描述
最近新学 Flask,想尝试使用蓝图模块化应用。但甚至在运行简易 demo 时就出错。
这个简易应用有两个文件:app.py
from flask import Flaskapp = Flask(__name__)from calendar import calendarapp.register_blueprint(calendar, url_prefix=’/auth’)@app.route(’/’)def hello_world(): return ’Hello World!’if __name__ == ’__main__’: app.run(port=8023)
calendar.py
from flask import Blueprint calendar = Blueprint(’calendar’, __name__) @calendar.route(’/’) def test():return ’yes’
运行时出现错误:
Traceback (most recent call last): File '/Users/vencent/.pyenv/versions/ec-env-3.6.0/lib/python3.6/site-packages/werkzeug/http.py', line 22, in <module> from email.utils import parsedate_tz File '/Users/vencent/.pyenv/versions/3.6.0/lib/python3.6/email/utils.py', line 33, in <module> from email._parseaddr import quote File '/Users/vencent/.pyenv/versions/3.6.0/lib/python3.6/email/_parseaddr.py', line 16, in <module> import time, calendar File '/Users/vencent/PycharmProjects/untitled1/calendar.py', line 1, in <module> from flask import BlueprintImportError: cannot import name ’Blueprint’During handling of the above exception, another exception occurred:Traceback (most recent call last): File '/Users/vencent/PycharmProjects/untitled1/untitled1.py', line 1, in <module> from flask import Flask File '/Users/vencent/.pyenv/versions/ec-env-3.6.0/lib/python3.6/site-packages/flask/__init__.py', line 17, in <module> from werkzeug.exceptions import abort File '/Users/vencent/.pyenv/versions/ec-env-3.6.0/lib/python3.6/site-packages/werkzeug/__init__.py', line 151, in <module> __import__(’werkzeug.exceptions’) File '/Users/vencent/.pyenv/versions/ec-env-3.6.0/lib/python3.6/site-packages/werkzeug/exceptions.py', line 71, in <module> from werkzeug.wrappers import Response File '/Users/vencent/.pyenv/versions/ec-env-3.6.0/lib/python3.6/site-packages/werkzeug/wrappers.py', line 26, in <module> from werkzeug.http import HTTP_STATUS_CODES, File '/Users/vencent/.pyenv/versions/ec-env-3.6.0/lib/python3.6/site-packages/werkzeug/http.py', line 24, in <module> from email.Utils import parsedate_tzModuleNotFoundError: No module named ’email.Utils’
之前尝试把已有的项目用 Blueprint 模块化也是这样的提示,当时以为是跟其他代码有冲突。今天试了一下发现这个 demo 都运行不了...
问题解答
回答1:werkzeug 的版本不兼容 python 3.6? 更新一下 werkzeug 试试。
相关文章:
1. css3 - css字体样式加填充色而不是背景色2. javascript - 小白求解:mac 下如何设置nodejs express的 NODE_NEV 环境变量3. Python如何考虑代码注入安全?4. html5 - 目前 公司App 嵌入H5页面 做个 手机支付功能 没有做过 所以 请求各位有经验的 给个思路5. css3中translate(-50%,-50%)对 transform-origin的奇葩影响?6. 安装sublime text 3 控制台的时候出现这个报错怎么办?7. 前端 - 请教一下CSS3中translateZ和rotateY书写顺序的问题8. javascript - 请问一下react-native 布局的时候,尺寸的大小是如何确定的呢?9. html5 - vue-cli 装好了 新建项目的好了,找不到项目是怎么回事?10. javascript - jQuery中live事件在移动微信端下没有效果;代码如下