Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing from main app in a flask blueprint

Tags:

python

flask

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
like image 641
Plasma Avatar asked May 16 '14 01:05

Plasma


1 Answers

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.

like image 68
davidism Avatar answered Oct 17 '22 09:10

davidism