title: Flask error 'ImportError: cannot import name 'db' from 'hello' (C:\Users\admin\flask_stuff\venv\hello.py)' when I try to create an SQL Database
body: <p>I'm somewhat new to Flask in Python and I'm following a YouTube tutorial by Corey Schafer. I'm currently on the 4th video in his tutorial, where he sets up a database with Flask SQLAlchemy. I have already entered the virtual environment for my app and ran the command <code>python</code>, and after that I use the command <code>from hello import db</code> (my app is called hello.py). However, when I run it, this is the error I get in my command prompt:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  ImportError: cannot import name 'db' from 'hello' (C:\Users\admin\flask_stuff\venv\hello.py)
  </code></pre>
  <p>I've tried to run the commands in my IDE itself (Visual Studio Code), but it doesn't seem to work there either. In VS Code, this is the issue I get instead when I run from Powershell:</p>
  <pre><code>  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;C:\Coding\flask_stuff\venv\hello.py&quot;, line 3, in &lt;module&gt;
      from flask_sqlalchemy import SQLAlchemy
  ModuleNotFoundError: No module named 'flask_sqlalchemy'
  </code></pre>
  <p>How can I fix this issue? The operating system I am running is Windows 10 64-bit.</p>
  <p>EDIT: Here is my code.</p>
  <pre><code>from datetime import datetime
  from flask import Flask, render_template, url_for, flash, redirect
  from flask_sqlalchemy import SQLAlchemy
  from forms import RegistrationForm, LoginForm
  
  app = Flask(__name__, template_folder='./templates')
  db = SQLAlchemy(app)
  app.config['SECRET_KEY'] = '3a3b2e821615d1661d6a0ff47b29cbdc'
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(20), unique=True, nullable=False)
      email = db.Column(db.String(120), unique=True, nullable=False)
      image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
      password = db.Column(db.String(60), nullable=False)
      posts = db.relationship('Post', backref='author', lazy=True)
  
      def __repr__(self):
          return f&quot;User('{self.username}', '{self.email}', '{self.image_file}')&quot;
  
  class Post(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(100), nullable=False)
      date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
      content = db.Column(db.Text, nullable=False)
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
  
      def __repr__(self):
          return f&quot;Post('{self.title}', '{self.date_posted}')&quot;
  
  
  posts = [
      {
          'author': 'QuadCore',
          'title': 'My First Post',
          'content': 'My first post content',
          'date_posted': 'February 2nd, 2021'
      },
      {
          'author': 'Mr.Names',
          'title': 'Computers 101',
          'content': 'My 2nd post',
          'date_posted': 'January 15th, 2021'
      }
  ]
  
  @app.route(&quot;/&quot;)
  @app.route(&quot;/home&quot;)
  def home():
      return render_template('home.html', posts=posts)
  
  @app.route(&quot;/about&quot;)
  def about():
      return render_template('about.html', title='About')
  
  @app.route(&quot;/register&quot;, methods=['GET', 'POST'])
  def register():
      form = RegistrationForm()
      if form.validate_on_submit():
          flash(f'{form.username.data} account creation successful.', 'success')
          return redirect(url_for('home'))
      return render_template('register.html', title='Register', form=form)
  @app.route(&quot;/login&quot;, methods=['GET', 'POST'])
  def login():
      form = LoginForm()
      if form.validate_on_submit():
          if form.email.data == 'admin@blog.com' and form.password.data == 'password':
              flash('Login successful!', 'success')
              return redirect(url_for('home'))
          else:
              flash('Login unsuccessful. Please check your information.', 'danger')
      return render_template('login.html', title='Login', form=form)
  
  if __name__==&quot;__main__&quot;:
      app.run(host= '0.0.0.0')
  </code></pre>