Error of migration, Flask

Asked

Viewed 137 times

-1

is appearing, "Nameerror: name 'app' is not defined" my codic

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///storage.db'
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app) 
manager.add_command('db', MigrateCommand)

from app.controllers import default

from app import manager


if __name__ == '__main__':
 manager.run()

1 answer

0

It became a little complicated to understand your code, but by the error to realize that your application "app" is not being defined, so the problem lies in creating the instance of the Flask object. From what I understand, you wrote:

app = Flask(name)

In fact, the right thing would be:

app = Flask(__name__)

That’s why he can’t create the app.

  • Remembering that name is a special python variable that references the script that is running, if that is the main script, then it will return the string main.

  • worse, which is app = flask(name)

  • I made some changes, some tests and now it appears : Usage: run.py [-? ] {db,shell,runserver} ... positional Arguments: {db,shell,runserver} db Perform database Migrations shell Runs a Python shell Inside Flask application context. runserver Runs the Flask Development server i.e. app.run() optional Arguments: -?, -help show this help message and Exit

  • Okay, come on, how are you running it? "python name.py" only? If you do this it will pass you the default commimands, because the manager is used so that you pass arguments through the command line, which can be the "runserver" that works as a "flask run", the "shell" that starts a python shell so that you can import what you need and run, or go straight through a decorator. Check out this doc link in the Defaults Commands section : https://flask-script.readthedocs.io/en/latest/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.