0Pricing
Flask Academy · Lesson

Init Extensions with init_app

Defer extension binding until app creation.

Init Extensions with init_app is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Extension Dilemma

Extensions like SQLAlchemy used to take the app right away. Inside a factory that does not work, because the app does not exist at import time yet. 🤔

Create Without an App

The fix is two steps. First, create the extension object at module level with no app passed in. It stays unbound for now.

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()  # no app yet

Bind It Later

Second, call init_app inside the factory once the app is built. This binds the extension to that specific instance.

def create_app():
    app = Flask(__name__)
    db.init_app(app)
    return app

Why Two Steps

The extension object can be imported anywhere as a global, while the actual binding waits until the factory chooses a config. Best of both worlds.

Importable Everywhere

Because db lives at module level, your models and views import it freely without touching the app, avoiding circular imports.

from app.extensions import db

A Tidy extensions Module

A common pattern keeps all unbound extensions in one extensions.py file, then the factory imports and inits each one.

db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()

Init Many at Once

Inside create_app you simply call init_app on each extension, passing the same fresh app to all of them.

db.init_app(app)
migrate.init_app(app, db)
login.init_app(app)

Per-App Binding

Because binding happens in the factory, two different apps can each init_app the same extension class independently. Great for tests.

Config Is Read at Init

Extensions read settings like the database URI during init_app, so set those config values before you bind them.

app.config["SQLALCHEMY_DATABASE_URI"] = uri
db.init_app(app)

Use It Within a Context

After init_app, the extension needs an app context to actually run queries, since it must know which app it is talking to.

with app.app_context():
    db.create_all()

The Golden Rule

Create the extension unbound at import, then call init_app in the factory. That single split unlocks the whole pattern. 🔑

Quick Check

Why create an extension object without an app, then call init_app?

Recap

You learned the two-step move: build extensions unbound, then call init_app in the factory. Importable globals plus per-app binding, with no circular imports. 🌟

Frequently asked questions

Is the “Init Extensions with init_app” lesson free?

Yes — the full text of “Init Extensions with init_app” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Init Extensions with init_app”?

Defer extension binding until app creation. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Init Extensions with init_app” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Problems with a Global app Object
  2. Write a create_app Function
  3. Init Extensions with init_app
  4. Register Blueprints in the Factory
← Back to Flask Academy