0Pricing
Flask Academy · Lektion

Einen Context in Skripten und Shells pushen

Führen Sie mit app_context Code außerhalb eines Requests aus.

Einen Context in Skripten und Shells pushen ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Outside the Web

Sometimes you run Flask code with no request, like a script that seeds the database. There is no context, so the usual proxies fail.

The Telltale Error

Run such code and Flask raises RuntimeError: working outside of application context. It is telling you no app context is active.

Push It Yourself

Fix it by pushing one with app.app_context(). Wrap your code in a with block and current_app starts working again.

with app.app_context():
    print(current_app.name)

What the with Block Does

Entering the with block pushes the context, and leaving it pops the context. You never have to clean up by hand.

Now Extensions Work

Inside the block, extensions bound to the app behave normally. You can run db.create_all() or query models from a plain script.

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

Need a Fake Request?

To exercise code that reads request, push a test request context with test_request_context instead of an app context.

with app.test_request_context("/hello"):
    print(request.path)

The Flask Shell

The flask shell command opens a Python shell with an app context already pushed, so you can poke at your app interactively.

flask shell

CLI Commands Too

Custom flask commands you register also run inside an app context automatically, so seed and admin scripts just work.

@app.cli.command()
def seed():
    db.create_all()

Match the Right Context

Pick by need: use app_context when you only touch the app or database, and a request context when your code reads request data.

Keep It Scoped

Push contexts in a tight with block, not for the whole script. Smaller scope means clearer code and predictable teardown.

Why This Matters

Pushing contexts lets you reuse app logic in migrations, cron jobs, and tests, not just inside live web requests.

Quick Check

Let us nail the fix for the context error.

Recap

No request? Push one. Use app_context for app and database work, test_request_context for request data, and let flask shell handle it for you. 🎉

Häufig gestellte Fragen

Ist die Lektion „Einen Context in Skripten und Shells pushen“ kostenlos?

Ja — der vollständige Text von „Einen Context in Skripten und Shells pushen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Einen Context in Skripten und Shells pushen“?

Führen Sie mit app_context Code außerhalb eines Requests aus. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Flask Academy zu starten?

Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Einen Context in Skripten und Shells pushen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?

Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. App Context und Request Context
  2. current_app und das g-Objekt
  3. Context Locals und Thread-Sicherheit
  4. Einen Context in Skripten und Shells pushen
← Zurück zu Flask Academy