Access config values in Flask from other files

Question:

I have the following app structure:

manage.py
myapp/
  __init__.py
  config.py
  views/
    __init__.py
    login.py
    ...

In myapp/__init__.py I have a function create_app() which returns the Flask app instance. The config values are also stated in create_app() too. I would like to be able to access these values in other files such as login.py. I’ve tried:

from myapp import create_app as app
print app.config['SECRET_KEY']

However I receive an error stating AttributeError: 'function' object has no attribute 'config'

What am I doing wrong and how can I fix this? Thanks.

Asked By: Pav Sidhu

||

Answers:

Use from flask import current_app. You define SECRET_KEY in settings.py.

print current_app.config['SECRET_KEY']

Answered By: goodcow

This worked for me, for the next person that needs this. I had a similar setup from the tutorials.

from myapp import create_app

app = create_app()
print app.config['SECRET_KEY']
Answered By: Jdougie
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.