Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"RuntimeError: working outside of application context" for config module

I have a config.py module that I call from my main file, it has a variety of app.configs for jinja2filters and some other plugins I'm using:

excerpt of config.py:

from flask import current_app as app  
#imports go here

def function1:
    print("hello")

app.config['PROPERTY_1'] = 'configgoeshere'

#jinja2 functions and configs would go here like
app.jinja_env.filters['datetime'] = datetimeformat

calling from index.py:

from flask import Flask, flash, render_template, jsonify, request, json, request, redirect, url_for, Response, send_from_directory

app = Flask(__name__)
app.config['DEBUG'] = False

app.config.from_object('config') #config file

############################################################################################################################
# MAIN ROUTES
############################################################################################################################
# The route for the homepage
@app.route('/')
def index():

returns the error:

RuntimeError: working outside of application context

Update I'm trying to pass the application context along now, here is index.py:

from flask import Flask, flash, render_template, jsonify, request, json, request, redirect, url_for, Response, send_from_directory
import time, datetime, os, urllib, urllib2, urlparse, requests, json, string
from urllib2 import Request, urlopen, URLError, HTTPError, urlparse

app = Flask(__name__)
app.config['DEBUG'] = False

app.config.from_object('config')


############################################################################################################################
# MAIN ROUTES
############################################################################################################################
# The route for the homepage
@app.route('/')
def index():

Here's the updated excerpt config.py:

from index import app
#imports go here

def function1:
    print("hello")

app.config['PROPERTY_1'] = 'configgoeshere'

#jinja2 functions and configs would go here like
app.jinja_env.filters['datetime'] = datetimeformat

The error returned is:

$ python index.py
Traceback (most recent call last):
  File "index.py", line 8, in <module>
    app.config.from_object('config')
  File "C:\Python27\lib\site-packages\flask\config.py", line 162, in from_object
    obj = import_string(obj)
  File "C:\Python27\lib\site-packages\werkzeug\utils.py", line 418, in import_string
    __import__(import_name)
  File "C:\Users\****\Desktop\*****\config.py", line 1, in <module>
    from index import app
  File "C:\Users\***\Desktop\*****\index.py", line 17, in <module>
    @app.route('/')
  File "C:\Python27\lib\site-packages\werkzeug\local.py", line 343, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Python27\lib\site-packages\werkzeug\local.py", line 302, in _get_current_object
    return self.__local()
  File "C:\Python27\lib\site-packages\flask\globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

Note - there is no code on line 162 inside of config.py like the error suggests

like image 815
24x7 Avatar asked Feb 06 '23 09:02

24x7


1 Answers

Check out Flask's explanation of Application Contexts.

In your config.py file, the from flask import current_app as app makes it so that the call to app.config['PROPERTY_1'] = 'configgoeshere' actually tries to set the config on current_app, though there's no application context by default until a request comes in (hence the error). Since that call is not within a function, it is executed immediately before anything else (like a request) can happen.

I would suggest doing the config on the app instance in index instead of on current_app.

like image 123
Karin Avatar answered Feb 13 '23 05:02

Karin