Push a Context in Scripts and Shells
Run code outside a request with app_context.
Push a Context in Scripts and Shells is a free Flask Academy lesson on CoddyKit — lesson 4 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.
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 shellCLI 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. 🎉
Frequently asked questions
Is the “Push a Context in Scripts and Shells” lesson free?
Yes — the full text of “Push a Context in Scripts and Shells” 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 “Push a Context in Scripts and Shells”?
Run code outside a request with app_context. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Push a Context in Scripts and Shells” 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
- App Context vs Request Context
- current_app and the g Object
- Context Locals and Thread Safety
- Push a Context in Scripts and Shells