Siecje Siecje - 1 year ago 214
Python Question

How to import from config file in Flask?

I have followed the layout of my Flask project from

I have the following structure:


In, I am able to use

from config import basedir

However I cannot use that in

I added

import Flask

and when I modify it, the Flask web server restarts, but it doesn't say found changes in app/ restarting it just restarts.

What do I need to do to be able to use

from config import basedir

in my python file. I don't see anything special being done in

EDIT: This is my

from flask import Flask
from config import basedir

app = Flask(__name__)
from app import views

Answer Source

When people talk about configs in Flask, they are generally talking about loading values into the app's configuration. In your above example you could have something like app.config.from_object('config') in your file. Then all the configuration values will be loaded into the app.config dictionary.

Then in any of your files you could just import the app object to gain access to that dictionary. I tend to access that app object by doing from flask import current_app as app then just app.config['MY_SETTING'] to get the value I care about. Read up more in the documenation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download