flask报错:flask.cli.NoAppException: Could not import app

原始代码:

webapp\__init__.py:

from flask import Flask
from webapp import views
app = Flask(__name__)

webapp\views.py:

from webapp import app
@app.route('/'):
	return 'Hello World!'

出错原因:

  • 设定应用 set FLASK_APP=webapp后,初始化首先执行__init__.py中的from webapp import views语句
  • 随即导入views.py,执行from webapp import app,需要从webapp中导入app,而此时app对象还未创建
  • 因此报错:flask.cli.NoAppException: Could not import app

解决方案:

webapp\__init__.py:

from flask import Flask
app = Flask(__name__)

from webapp import views

你可能感兴趣的:(Python)