title: How to fix "RuntimeError: Working outside of application context." when creating blueprints with Flask?
body: <p>I'm trying to create a Blueprint and ran into this problem:</p>
  <pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
    File &quot;C:\Users\Max\PycharmProjects\python1\flask_first\__init__.py&quot;, line 3, in &lt;module&gt;
      from models import db
    File &quot;C:\Users\Max\PycharmProjects\python1\flask_first\models.py&quot;, line 5, in &lt;module&gt;
      current_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3.html'  # access to the SQL
    File &quot;C:\python3.9\lib\site-packages\werkzeug\local.py&quot;, line 347, in __getattr__
      return getattr(self._get_current_object(), name)
    File &quot;C:\python3.9\lib\site-packages\werkzeug\local.py&quot;, line 306, in _get_current_object
      return self.__local()
    File &quot;C:\python3.9\lib\site-packages\flask\globals.py&quot;, line 52, in _find_app
      raise RuntimeError(_app_ctx_err_msg)
  RuntimeError: Working outside of application context.
  
  This typically means that you attempted to use functionality that needed
  to interface with the current application object in some way. To solve
  this, set up an application context with app.app_context().  See the
  documentation for more information.
  </code></pre>
  <p>I've already done a lot of research and nothing works for me (or I'm just not looking properly enough).</p>
  <p>This is the <code>models.py</code> code:</p>
  <pre><code>from flask_sqlalchemy import SQLAlchemy
  from flask import current_app
  
  
  current_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3.html'  # access to the SQL
  current_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  db = SQLAlchemy(current_app)
  
  class users(db.Model):
      __tablename__ = 'users'
      _id = db.Column('id', db.Integer, primary_key=True)
      name = db.Column(db.String(80))
      email = db.Column(db.String(120))
      password = db.Column(db.Integer)
  
      def __init__(self, name, email, password):
          self.name = name
          self.email = email
          self.password = password
  </code></pre>
  <p>And this is the <code>__init__.py</code>:</p>
  <pre><code>from datetime import timedelta
  from flask import Flask
  from models import db
  from flask_first.admin.second import second
  
  def create_app():
      app = Flask(__name__)
      with app.app_context():
  
          db.init_app(app)
      return app
  
  create_app.secret_key = 'hello world'
  create_app.permanent_session_lifetime = timedelta(minutes=5)  # setting the time for long-lasting session
  
  if __name__ == '__main__':
      db.create_all()
      create_app.run(debug=True)
  </code></pre>
  <p>Here is a screenshot of my structure:<br />
  <a href="https://i.stack.imgur.com/lKb30.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lKb30.png" alt="file/folder structure" /></a></p>