Flask blueprint cannot read sqlite3 DATABASES from config file
<p>I would like Python Flask to read from configuration file the location of the sqlite3 database name <strong>without explicitly writing database name</strong>. Templates used are:  <a href="http://flask.pocoo.org/docs/0.11/patterns/sqlite3/" rel="nofollow">http://flask.pocoo.org/docs/0.11/patterns/sqlite3/</a>  and  <a href="http://flask.pocoo.org/docs/0.11/tutorial/dbcon/" rel="nofollow">http://flask.pocoo.org/docs/0.11/tutorial/dbcon/</a>.</p>

<p>When I try to read 'DATABASE' from my config file I get the following error message:</p>

<p>File "/app/my_cool_app/app/<strong>init</strong>.py", line 42, in before_request
    g.db = connect_db()</p>

<p>File "/app/my_cool_app/app/<strong>init</strong>.py", line 36, in connect_db
    return sqlite3.connect(my_cool_app.config['DATABASE'])</p>

<p>AttributeError: 'Blueprint' object has no attribute 'config'</p>

<p>Here is my <strong>init</strong>.py code when I try to read from the configuration file and get the above error:</p>

<pre><code>import sqlite3
from flask import Flask, g
from .views import my_cool_app

# create application
def create_app(debug=True):
    app = Flask(__name__, instance_relative_config=True)
    app.debug = debug
    app.config.from_object('config')
    app.config.from_pyfile('config.py')
    app.register_blueprint(my_cool_app)
    return app

def connect_db():
    return sqlite3.connect(my_cool_app.config['DATABASE'])  &lt;= LINE 36

@my_cool_app.before_request
def before_request():
    g.db = connect_db()

@my_cool_app.teardown_request
def teardown_request(exception):
    db = getattr(g, 'db', None)
    if db is not None:
        db.close()
</code></pre>

<p>Here is my run.py (I don't change it):</p>

<pre><code>from app import create_app
app = create_app()
</code></pre>

<p>Here is my <strong>init</strong>.py code that works when I explicitly write DB name (not what I want):</p>

<pre><code>import sqlite3
from flask import Flask, g
from .views import my_cool_app
DATABASE='/app/myappname/my_sqlite3_database_name.db'

# create application
def create_app(debug=True):
    app = Flask(__name__, instance_relative_config=True)
    app.debug = debug
    app.config.from_object('config')
    app.config.from_pyfile('config.py')
    app.register_blueprint(my_cool_app)
    return app

def connect_db():
    return sqlite3.connect(DATABASE)
</code></pre>

<p>Your <code>my_cool_app</code> is an instance of <code>Blueprint</code> which doesn't have a <code>config</code> attribute. You need to use <code>current_app</code>:</p>

<pre><code>import sqlite3
from flask import Flask, g, current_app
from .views import my_cool_app

# create application
def create_app(debug=True):
    app = Flask(__name__, instance_relative_config=True)
    app.debug = debug
    app.config.from_object('config')
    app.config.from_pyfile('config.py')
    app.register_blueprint(my_cool_app)
    return app

def connect_db():
    return sqlite3.connect(current_app.config['DATABASE'])

@my_cool_app.before_request
def before_request():
    g.db = connect_db()

@my_cool_app.teardown_request
def teardown_request(exception):
    db = getattr(g, 'db', None)
    if db is not None:
        db.close()
</code></pre>

python flask sqlite3 blueprint