Running Python by interactive mode when using Flask factory pattern

Taehwan Kim
Aug 28, 2017 · 1 min read

When we are using Flask factory pattern, we can divide configuration and implementation, also declare dependencies easily.

But I cannot use my app object and model classes directly in interactive mode when using factory pattern.

>>> from app.models import User, Board
>>> Board.query.search('foo')
...
RuntimeError: application not registered on db instance and no applicationbound to current context

How can I resolve this RuntimeError? Exception message tells a solution. We should run that codes at context of application. Please see below code blocks.

app = create_app()with app.app_context():
result = Board.query.search('asd')

It didn’t show exception. First create flask application by create_app() and cover your code with app_context() when you are using interactive mode.

)
Taehwan Kim

Written by

Python Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade