Flask Shell Commands not working
Solution 1
Thanks a lot to Miguel, the writer of the FLASK Mega Tutorial (go check that out) wo solved my problem!
As he pointed out in a comment below my question: you cannot have a module and a package with the same name. So no application folder and application.py at the same time.
Solution:
I changed my 'application.py into 'theapp.py' and now flask shell works like a charm! I did not need to change anything in my files, apart from running export FLASK_APP=theapp.py in the terminal.
Solution 2
Flask needs to be told how to import it, by setting the
FLASK_APP:(venv) $ export FLASK_APP=microblog.py
If you are using Microsoft Windows, use set instead of export in the command above.
Related videos on Youtube
StackTT
Updated on September 26, 2022Comments
-
StackTT 3 months
I'm new to python and flask and currently working on the Flask Mega-Tutorial, however: I'm stuck getting
flask shellto recognize my custom symbols/commands.When I try to access the model
Useras a symbol by typingflask shellin my virtual environment, I getNameError: name 'User' is not defined.Usershould return:<class 'application.models.User'>, but shows the error instead.What I don't understand is that the
appsymbol seems to work fine and returns<Flask 'application'>as it should.What am I doing wrong here? Something with the imports?
I did some research: this looks like my problem but does not use the
app.sell_context_processordecorator.I tried also variations of my code: both changing import names from app to application as I changed these from the default in the tutorial and using
userinstead ofUser(lower vs. uppercase), but nothing seems to work.Please help me fix this!
Error
(venv) MBP:books mbpp$ flask shell Python 3.6.5 (default, Mar 30 2018, 06:42:10) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin App: application Instance: /Users/mbpp/Sites/books/instance >>> app <Flask 'application'> >>> User Traceback (most recent call last): File "<console>", line 1, in <module> NameError: name 'User' is not definedMy code from application.py
from application import app, db from application.models import User, Book, State, Course, BookTitle, Author @app.shell_context_processor def make_shell_context(): return {'db': db, 'User': User, 'State': State, 'BookTitle': BookTitle, 'Author': Author}and from __init__.py
from flask import Flask from config import Config from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask_login import LoginManager # initiate the Flask app app = Flask(__name__) # use the config.py file for configuration app.config.from_object(Config) # use SQLAlchemy for database management db = SQLAlchemy(app) # use Flask-Migrate extension for database migration management migrate = Migrate(app, db) # use Flask-Login extension for login form login = LoginManager(app) login.login_view = 'login' from application import routes, modelsand from models.py (I'm building a website where users can sell books)
from application import db, login from datetime import datetime from werkzeug.security import generate_password_hash, check_password_hash from flask_login import UserMixin # create a table to store users class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(64), index = True, unique = True) email = db.Column(db.String(120), index = True, unique = True) password_hash = db.Column(db.String(128)) phone = db.Column(db.String(64)) books = db.relationship('Book', backref='seller_name', lazy='dynamic') def __repr__(self): return '<User: {}>'.format(self.username) # create a password hash def set_password(self, password): self.password_hash = generate_password_hash(password) # check the password hash against a user given password def check_password(self, password): return check_password_hash(self.password_hash, password) # create a table to store information on a book for sale class Book(db.Model): id = db.Column(db.Integer, primary_key = True) course_id = db.Column(db.Integer, db.ForeignKey('course.id')) title = db.Column(db.Integer, db.ForeignKey('booktitle.id')) author = db.Column(db.Integer, db.ForeignKey('author.id')) price = db.Column(db.Integer) isbn = db.Column(db.String(64), index = True) state_id = db.Column(db.Integer, db.ForeignKey('state.id')) state_description = db.Column(db.String(256)) seller_id = db.Column(db.Integer, db.ForeignKey('user.id')) creation_timestamp = db.Column(db.DateTime, index = True, default = datetime.utcnow) def __repr__(self): return '<Book: {}>'.format(self.title) # create a table to store different states books can be in class State(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(128), index = True) books = db.relationship('Book', backref='state', lazy='dynamic') def __repr__(self): return '<State: {}>'.format(self.name) # create a table to store courses class Course(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(128), index = True) year = db.Column(db.Integer, index = True) books = db.relationship('Book', backref='course', lazy='dynamic') def __repr__(self): return '<Course: {}>'.format(self.name) # create a table to store booktitles class BookTitle(db.Model): id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(128), index = True) books = db.relationship('Book', backref='book_title', lazy='dynamic') def __repr__(self): return '<Book title: {}>'.format(self.title) # create a table to store authors class Author(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(128), index = True) books = db.relationship('Book', backref='author_name', lazy='dynamic') def __repr__(self): return '<Author: {}>'.format(self.name) # user loader for Flask-Login extension, gets users ID @login.user_loader def load_user(id): return User.query.get(int(id))-
Miguel Grinberg over 4 years@StackTT what is the value of your
FLASK_APPenvironment variable?
-
-
Dennis Braga over 3 yearsThis one have worked for me as well. It should be included as a warning on the tutorial, though. -
CoderGuy123 over 3 yearsDoes not work for me. I had also usedmicroblogfor both the script and the dir. Changed the script to app.py and fixed the references. flask shell does not register it and the environment is set. I checked withfrom envs import envthenenv("FLASK_APP")which prints'app.py'. It did also not work by settingexport FLASK_APP=app.pyas an alternative method. -
CoderGuy123 over 3 yearsFound the issue. By comparing with the premade code, I found that myapp.pyfile should be in the main dir, not inside theappdir. -
Joe over 2 yearsFurther info on the background: stackoverflow.com/questions/6393861/… -
user3680029 about 1 yeardepending on your application set up, you can use a "manage.py" script for managing some setup including the shell context you need a @app.shell_context_processor decorator and make_shell_context() function which returns a "context" dictionnary mapping from "Model" Object to "Context" Object (making them visible in the shell context)