working outside of application context - Flask

flask context processor
runtimeerror working outside of application context. celery
flask global variable
flask sqlalchemy working outside of application context
flask-sqlalchemy app context
flask redis app context
flask current_app config
flask blueprint
def get_db(self,dbfile):
    if hasattr(g, 'sqlite_db'): self.close_db(g.sqlite_db)
    try:
        g.sqlite_db = self.connect_db('{}/{}'.format(app.root_path, dbfile))
    except sqlite3.OperationalError as e:
        raise e

    return g.sqlite_db

Hi this code is located inside DB class, The error I get is

RuntimeError: working outside of application context

the error occurs on this line

g.sqlite_db = self.connect_db('{}/{}'.format(app.root_path, dbfile))

I think the problem is with g, it is imported like that from flask import g

How this error can be fixed? Thanks.

From the Flask source code in flask/globals.py:

_app_ctx_err_msg = '''\
Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in a way.  To solve
this set up an application context with app.app_context().  See the
documentation for more information.\
'''

Following the documentation, you can see that you need to make flask.current_app point to your application and it currently doesn't.

You're probably calling your DB function before Flask has initialized. My guess is that your app object has not been created yet with the Flask constructor.

RuntimeError: working outside of application context, Flask has an Application Context, and it seems like you'll need to do something like: def test_connection(self): with app.app_context(): #test  Yes if you have not specified an application context you should create it. This is because it is necessary to define all the necessary resources within the Flask application. The documentation explains perfectly in which cases and how you should use a context application in Flask.

Maybe you need to call your function inside an application context:

with app.app_context():
  # call your method here

RuntimeError: working outside of application context, Breaking down the nuances of the 'app context' in Flask's Application this throws RuntimeError: Working outside of application context. Flask has an Application Context, and it seems like you'll need to do something like:. def test_connection(self): with app.app_context(): #test code You can probably also shove the app.app_context() call into a test setup method as well.

When creating your app, use:

app.app_context().push()

for example like this:

from yourapp import create_app

app = create_app()

app.app_context().push()

for further information

Demystifying Flask's Application Context, When using the mailer to send an e-mail from a background worker (that is, outside of the Flask application context), it crashes and burns. This is incorrect, Flask has no way to figure out what is your application instance. What you need to do is use the real application instance instead of current_app. You can use current_app inside a view function because Flask sets the app context for you, but you cannot use it in the global scope of your application.

To expand on @VadimK's answer. If you want to prevent your code from executing outside of an app_context you can use flask.has_app_context() to see if the code is currently inside an app context:

See also: flask.has_request_context()

RuntimeError: working outside of application context · Issue #63 , I'm using Flask in the structure defined here and am coming across this error: Traceback (most recent call last): File "run.py", line 2, in from app  i know there are few question on how to deal with flask "working outside of application context", but i could not get them to work for me. I have a long running mongo aggregation queries and are scheduled to run at regular intervals using apscheduler.

I had the same issue while doing some unit testing.

Adding the following function to my test class solved my issue:

@classmethod
def setUpClass(self):
    self.app = create_app("testing")
    self.client = self.app.test_client()

RuntimeError: working outside of application context · Issue #8 , It starts when the Flask object is instantiated, and it implicitly ends when the first request comes in. RuntimeError: Working outside of application context. When using the mailer to send an e-mail from a background worker (that is, outside of the Flask application context), it crashes and burns. When using the mailer to send an e-mail from a background worker (that is, outside of the Flask application context), it crashes and burns.

The Application Context, Flask uses contexts to make temporarily certain variables globally accessible. If you are RuntimeError: Working outside of application context. This typically  If you are working inside a Flask view function or a CLI command, that automatically happens. However, if you are working inside the interactive shell, you will have to do that yourself (see Creating an Application Context ).

Contexts in Flask, py", line 44, in _lookup_app_object raise RuntimeError(_app_ctx_err_msg) RuntimeError: Working outside of application context. This typically means that you  Given that an application can be running in multiple threads and serving multiple clients at once, I assume that this kind of context management will prove very helpful in keeping things organized. First, let’s take a look at the application context. The Application Context. An excerpt from the Flask docs:

RuntimeError: Working outside of application context flask : flask, def run(): app = Flask(__name__, static_folder=None) setup_database(app) function - it throws "RuntimeError: Working outside of application context". from flask import Flask, g app = Flask(__name__) with app.app_context(): g.my_db = 'database ok' print(g.my_db) this throws RuntimeError: Working outside of application context. Even if you create a new context: from flask import Flask, g app = Flask(__name__) with app.app_context(): g.my_db = 'database ok' with app.app_context(): print(g.my_db)

Comments
  • Downvote. This answer provides nothing beyond what the user already sees when the error occurs.