flask - NameError: name 'app' is not defined

前端 未结 2 2014
花落未央
花落未央 2020-12-31 12:15

I\'m running Flask through thescript, below (run.py)

#!flask/bin/python
from app import app
app.run(debug=True)

When runni

相关标签:
2条回答
  • 2020-12-31 12:42

    This error is because of you are not defining app and directly using app

    Solution is add this line in your code : app Flask(__name__)

    Example: app.py

    from flask import Flask
    #You need to use following line [app Flask(__name__]
    app = Flask(__name__)
    @app.route('/')
    def index():
        return "Hello World with flask"
    if __name__ == '__main__':
        app.run(port=5000,debug=True)
    
    0 讨论(0)
  • 2020-12-31 12:58

    You are using the app before importing it, here lm.init_app(app), app is not defined yet.

    It should look like this:

    from flask import Flask
    from flask.ext.sqlalchemy import SQLAlchemy
    import os
    
    from flask.ext.login import LoginManager
    from flask.ext.openid import OpenID
    from config import basedir
    
    app = Flask(__name__)
    app.config.from_object('config')
    
    lm= LoginManager()
    lm.init_app(app)
    oid = OpenID(app,os.path.join(basedir,'tmp'))
    lm.login_view = 'login'
    
    db = SQLAlchemy(app)
    
    from app import views, models
    
    0 讨论(0)
提交回复
热议问题