title: Flask Shell Commands not working
body: <p>I'm new to python and flask and currently working on the <a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database" rel="noreferrer">Flask Mega-Tutorial</a>, however: I'm stuck getting <code>flask shell</code> to recognize my custom symbols/commands. </p>
  
  <p>When I try to access the model <code>User</code> as a symbol by typing <code>flask shell</code> in my virtual environment, I get <code>NameError: name 'User' is not defined</code>. </p>
  
  <p><code>User</code> should return: <code>&lt;class 'application.models.User'&gt;</code>, but shows the error instead.</p>
  
  <p>What I don't understand is that the <code>app</code> symbol seems to work fine and returns <code>&lt;Flask 'application'&gt;</code> as it should.</p>
  
  <p><em>What am I doing wrong here? Something with the imports?</em></p>
  
  <p>I did some research: <a href="https://stackoverflow.com/questions/42005343/accessing-user-model-from-flask-shell-raises-nameerror">this</a> looks like my problem but does not use the <code>app.sell_context_processor</code> decorator.</p>
  
  <p>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 <code>user</code> instead of <code>User</code> (lower vs. uppercase), but nothing seems to work. </p>
  
  <p>Please help me fix this!</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>    (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
      &gt;&gt;&gt; app
      &lt;Flask 'application'&gt;
      &gt;&gt;&gt; User
      Traceback (most recent call last):
        File "&lt;console&gt;", line 1, in &lt;module&gt;
      NameError: name 'User' is not defined
  </code></pre>
  
  <p><strong>My code</strong> from <em>application.py</em></p>
  
  <pre><code>   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}
  </code></pre>
  
  <p>and from <strong><em>__init__.py</em></strong></p>
  
  <pre><code>    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, models
  </code></pre>
  
  <p>and from <strong><em>models.py</em></strong> (I'm building a website where users can sell books)</p>
  
  <pre><code>   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 '&lt;User: {}&gt;'.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 '&lt;Book: {}&gt;'.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 '&lt;State: {}&gt;'.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 '&lt;Course: {}&gt;'.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 '&lt;Book title: {}&gt;'.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 '&lt;Author: {}&gt;'.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))
  </code></pre>