Importing from main app in a flask blueprint

Plasma

I'm writing an application with one blueprint. My application uses Flask-SQLAlchemy, so my blueprint needs access to the main app's db object (created by Flask-SQLAlchemy) in order to create its own models.

However, when I try to get the db object with current_app.db, flask gives me the following error:

RuntimeError: working outside of application context

Here is my main __init__.py:

from flask import Flask

from app.uploader import uploader

app = Flask(__name__)

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)

app.register_blueprint(uploader)

Here's the __init__.py from my uploader blueprint:

from flask import Blueprint

uploader = Blueprint('uploader', __name__,
    template_folder='templates')

from . import views
from .models import *

Here's views.py of the blueprint, where the exception takes place:

from flask import (redirect, render_template, request, send_from_directory,
    session, current_app)
from flask.views import View
from werkzeug import secure_filename

print current_app.db # Exception happens here

And here's the stacktrace:

Traceback (most recent call last):
  File "runtests.py", line 11, in <module>
    import tests
  File "/home/plasmasheep/project/tests.py", line 14, in <module>
    from app import app, db, user_datastore
  File "/home/plasmasheep/project/app/__init__.py", line 6, in <module>
    from app.uploader import uploader
  File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
    from . import views
  File "/home/plasmasheep/project/app/uploader/views.py", line 18, in <module>
    print current_app.db
  File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

Simply trying to use from .. import db does not work:

Traceback (most recent call last):
  File "runtests.py", line 11, in <module>
    import tests
  File "/home/plasmasheep/project/tests.py", line 14, in <module>
    from app import app, db, user_datastore
  File "/home/plasmasheep/project/app/__init__.py", line 7, in <module>
    from app.uploader import uploader
  File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
    from . import views
  File "/home/plasmasheep/project/app/uploader/views.py", line 17, in <module>
    from .. import db
ImportError: cannot import name db

Nor does from app import db:

Traceback (most recent call last):
  File "runtests.py", line 11, in <module>
    import tests
  File "/home/plasmasheep/project/tests.py", line 14, in <module>
    from app import app, db, user_datastore
  File "/home/plasmasheep/project/app/__init__.py", line 7, in <module>
    from app.uploader import uploader
  File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
    from . import views
  File "/home/plasmasheep/project/app/uploader/views.py", line 17, in <module>
    from app import db
ImportError: cannot import name db
davidism

current_app is only set during (essentially) a request/response cycle. Normally, you use this only inside views, or stuff that is guaranteed to be called inside views. You typically use current_app when you don't have access to the app directly, such as if you are using an application factory. Since you're not using a factory, just import db directly and it should work in your case.

The import error is due to a circular import. Move the line from app.uploader import uploader to after the definition of db. See a couple paragraphs into this section of the docs, which mentions importing views after defining any of their dependencies.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flask: Template in Blueprint Inherit from Template in App?

From Dev

Fix cicular import when importing Flask blueprint

From Dev

Calling method from Flask Blueprint

From Dev

Running a Flask blueprint from the program

From Dev

What does it mean to register a blueprint into the app in Flask?

From Java

In Flask: How to access app Logger within Blueprint

From Dev

Problems in importing modules in flask app

From Dev

How to run Flask app by flask run with blueprint template

From Dev

ModuleNotFoundError in flask app while importing 'app' folder

From Dev

Importing from __init__ in Flask

From Dev

Retrieving config from a blueprint in Sanic app

From Dev

AssertionError in Flask App when connecting to database using config in blueprint

From Java

Using Flask-SQLAlchemy in Blueprint models without reference to the app

From Java

Using Flask-SQLAlchemy in Blueprint models without reference to the app

From Dev

Python Flask can't access app inside Blueprint

From Dev

Importing modules from parent folder does not work when running flask app

From Dev

Extending Flask class as main App

From Dev

Getting flask-restful routes from within a blueprint

From Dev

Creating Flask-SQLAlchemy instance with metadata from Blueprint

From Dev

importing variable from main file to class variable

From Dev

importing other file and using functions from main

From Dev

Flask Session not persistant in blueprint

From Dev

Inject dependencies in Flask blueprint

From Dev

Overwrite route in flask blueprint

From Dev

Flask Blueprint 404

From Java

flask blueprint template folder

From Dev

Flask Blueprint structure

From Dev

Flask - Forms - Blueprint

From Dev

Flask blueprint and registering path

Related Related

HotTag

Archive