close

[Solved] flask – Name: name ‘app’ is not defined

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error flask – Name: name ‘app’ is not defined in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How flask – Name: name ‘app’ is not defined Error Occurs?

Today I get the following error flask – Name: name ‘app’ is not defined in python.

How To Solve flask – Name: name ‘app’ is not defined Error ?

  1. How To Solve flask – Name: name 'app' is not defined Error ?

    To Solve flask – Name: name 'app' is not defined Error You are using the app before importing it, here lm.init_app(app), app is not defined yet.

  2. flask – Name: name 'app' is not defined

    To Solve flask – Name: name 'app' is not defined Error You are using the app before importing it, here lm.init_app(app), app is not defined yet.

Solution 1

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)

Solution 2

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read