python - Flask menggunakan Blueprint dan ImportError: tidak boleh mengimport nama 'Blueprint' berlaku?
迷茫
迷茫 2017-05-18 10:52:28
0
1
1455

Saya baru-baru ini mempelajari Flask dan ingin mencuba menggunakan cetak biru untuk aplikasi modular. Tetapi walaupun menjalankan demo mudah ia menjadi salah.

Aplikasi mudah ini mempunyai dua fail:
app.py

from flask import Flask

app = Flask(__name__)
from calendar import calendar
app.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'

Ralat berlaku semasa menjalankan:

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 Blueprint
ImportError: 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_tz
ModuleNotFoundError: No module named 'email.Utils'

Saya juga mendapat gesaan yang sama apabila saya cuba menggunakan Blueprint untuk memodulasi projek sedia ada Pada masa itu, saya fikir ia bercanggah dengan kod lain. Saya mencubanya hari ini dan mendapati bahawa demo tidak dapat dijalankan...

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

membalas semua(1)
刘奇

Versi werkzeug tidak serasi dengan python 3.6? Cuba kemas kini werkzeug.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan