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.

4 Answers 4

63

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

print current_app.config['SECRET_KEY']

5
  • I have no settings.py file and I would rather set all the config values in config.py instead.
    – Pav Sidhu
    Aug 14, 2015 at 20:54
  • 17
    I tried the current_app method and receive this: RuntimeError: working outside of application context
    – Pav Sidhu
    Aug 14, 2015 at 21:17
  • 3
    Make sure you initialize the config file with app.config.from_object('myapp.config') as shown in flask.pocoo.org/docs/0.10/config
    – goodcow
    Aug 15, 2015 at 3: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 Dec 3, 2017 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, 2020 at 4: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']
1
  • This works if using the application factory
    – jshwi
    May 17, 2022 at 4:39
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.

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'])

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.