Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access config values in Flask from other files

Tags:

python

flask

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.

like image 433
Pav Sidhu Avatar asked Aug 14 '15 19:08

Pav Sidhu


People also ask

How do I import a config file into a Flask?

To import from config file in Python Flask, we can use the config. from_object method. to create config classes in the config.py file. in app.py to load the config from the ProductionConfig class in the config.py file with the app.

How do I use .ENV files in Flask?

env file inside the Python Flask application. Reads the key-value pair from the . env file and adds them to the environment variable. It is great for managing app settings during development and in production using 12-factor principles.

What is app config [' SECRET_KEY '] in Flask?

In order to use session in flask you need to set the secret key in your application settings. secret key is a random key used to encrypt your cookies and save send them to the browser. To fix this you just need to set a SECRET_KEY in your config file.


1 Answers

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

print current_app.config['SECRET_KEY']

like image 144
goodcow Avatar answered Oct 04 '22 01:10

goodcow