32

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.

Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110

4 Answers4

63

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

print current_app.config['SECRET_KEY']

goodcow
  • 4,495
  • 6
  • 33
  • 52
  • I have no `settings.py` file and I would rather set all the config values in `config.py` instead. – Pav Sidhu Aug 14 '15 at 20:54
  • 17
    I tried the current_app method and receive this: `RuntimeError: working outside of application context` – Pav Sidhu Aug 14 '15 at 21:17
  • 3
    Make sure you initialize the config file with `app.config.from_object('myapp.config')` as shown in http://flask.pocoo.org/docs/0.10/config/ – goodcow Aug 15 '15 at 03:55
  • 8
    To avoid `RuntimeError: Working outside of application context.` make sure that you are calling `current_app` within the context of a request. See [here](http://flask.pocoo.org/docs/0.12/appcontext/#creating-an-application-context) – Christian Groleau Dec 03 '17 at 21:34
  • When I check the address of app in main python file and current_app in sub file, the values are different. Doesn't it matter? – uthline Jan 15 '20 at 04:52
1

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']
Jdougie
  • 21
  • 2
0

Two scenerios:

1. Your code is within app scope:

Add this to top of your file from flask import current_app

To access variables saved to your app config: current_app.config['SECRET_KEY'] or current_app.config.get('SECRET_KEY')

Make sure you called app.config.from_object(config) in your app or _init_ py file or initialize your app.config in a different manner.

2. Your code is outside app scope or it is being compiled before app is initialized. There are a few options.

a. Import your secret key from your environment.

import os

SECRET_KEY = os.getenv("SECRET_KEY", "defaultSecret")

b. Import your secret key from a local file:

from local import config

SECRET_KEY = config.get("SECRET_KEY")

Where local is the file that contains your configuration class.

Logan Cundiff
  • 479
  • 8
  • 13
0

Create the app with app context and init your db and other dependencies

def create_app():
app = Flask(__name__, static_folder="assets")
cfg = import_string(environ.get('PROFILE'))()
app.config.from_object(cfg)

with app.app_context():
   init_db()

then you can use

from flask import current_app

def init_db(self):
  mongo_client = MongoClient(current_app.config['DATABASE_URI'])
Nirbhay Rana
  • 4,229
  • 2
  • 18
  • 4