Getting config variables outside of the application context
If you are using a configuration pattern utilising classes and inheritance as described here, you could simply import your config classes with their respective properties and access them anywhere you want:
class Config(object): IMPORT = 'ME' DEBUG = False TESTING = False DATABASE_URI = 'sqlite:///:memory:'class ProductionConfig(Config): DATABASE_URI = 'mysql://user@localhost/foo'class DevelopmentConfig(Config): DEBUG = Trueclass TestingConfig(Config): TESTING = True
Now, in your foo.py
:
from config import Configprint(Config.IMPORT) # prints 'ME'
well, since current_app
can be a proxy of your flask program when the blueprint is registered, and that is done at run-time, you can't use it in your models_package
modules.(app tries to import models_package
, and models_package
requires app's configs to initalize things- thus import fails)
one option would be doing circular imports :
assuming everything is in 'App' module:
__init__.py
import flaskapplication = flask.Flask(__name__)application.config = #load configsimport models_package
models_package.py
from App import applicationconfig = application.config
or create your own config
object, but that somehow doubles complexity:
models_package.py
import flaskconfig = flask.config.Config(defaults=flask.Flask.default_config)#pick one of those and apply the same config initialization as you do in #your __init__.pyconfig.from_pyfile(..) #orconfig.from_object(..) #orconfig.from_envvar(..)