RuntimeError: Working outside of application context

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)

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