# RuntimeError: Working outside of application context

**URL:** <https://forum.freecodecamp.org/t/runtimeerror-working-outside-of-application-context/579047>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [December 20, 2022, 12:49am UTC](https://forum.freecodecamp.org/t/runtimeerror-working-outside-of-application-context/579047 "2022-12-20T00:49:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dumbandgeeky](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dumbandgeeky/32/270260_2.png) [@dumbandgeeky](https://forum.freecodecamp.org/u/dumbandgeeky)\
**Post date:** [December 20, 2022, 12:49am UTC](https://forum.freecodecamp.org/t/runtimeerror-working-outside-of-application-context/579047/1 "2022-12-20T00:49:38Z")

</div>

I have a small app that is using sql as a database and its throwing the error:

RuntimeError: Working outside of application context.

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

I have tried using with “app.context()” and “flask.current\_app”. I am unsure what to do to solve this error.

This is my main.py:  
import os  
from flask import Flask  
from flask\_restful import Resource, Api  
from application import config  
from application.config import LocalDevelopmentConfig  
from application.database import db

app = None  
api = None

def create\_app():  
app = Flask( **name** , template\_folder=“templates”)  
if os.getenv(‘ENV’, “development”) == “production”:  
raise Exception(“Currently no production config is setup.”)  
else:  
print(“Staring Local Development”)  
app.config.from\_object(LocalDevelopmentConfig)  
db.init\_app(app)  
app.app\_context().push()  
api = Api(app)  
return app, api

app, api = create\_app()

# Import all the controllers so they are loaded

from application.controllers import \*

from application.api import UserAPI

api.add\_resource(UserAPI, ‘/api/user’, “/api/user/string:username”)

if **name** == ‘ **main** ’:  
# Run the Flask app  
app.run(host=‘0.0.0.0’, port=8000)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [June 20, 2023, 12:49pm UTC](https://forum.freecodecamp.org/t/runtimeerror-working-outside-of-application-context/579047/2 "2023-06-20T12:49:59Z")

</div>

Closed on June 20, 2023, 12:49pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
