Advanced Usage

Yeah Dynamic is part of the name of this library so you can do lots of things :)

Customizing the settings object

Sometimes you want to override settings for your existing Package or Framework lets say you have a conf module exposing a config object and used to do:

from myprogram.conf import config

Now you want to use Dynaconf, open that or conf/ and do:

# coding: utf-8
from dynaconf import LazySettings


Now you can use export MYPROGRAM_FOO=bar instead of DYNACONF_FOO=bar

Switching working environments

To switch the environment programatically you can use setenv or using_env.

Using context manager

from dynaconf import settings

with settings.using_env('envname'):
    # now values comes from [envmane] section of config
    assert settings.HOST == '

Using env setter

from dynaconf import settings

# now values comes from [envmane] section of config
assert settings.HOST == ''

# now working env are back to previous

Populating objects

New in 2.0.0

You can use dynaconf values to populate Python objects (intances).


class Obj:

then you can do:

from dynaconf import settings  # assume it has DEBUG=True and VALUE=42.1
obj = Obj()


assert obj.DEBUG is True
assert obj.VALUE == 42.1

Also you can specify only some keys:

from dynaconf import settings  # assume it has DEBUG=True and VALUE=42.1
obj = Obj()

settings.populate_obj(obj, keys=['DEBUG'])

assert obj.DEBUG is True  # ok

assert obj.VALUE == 42.1  # AttributeError


It is possible to customize how your project will load settings, example: You want your users to customize a settings file defined in export PROJECTNAME_SETTINGS=/path/to/settings.toml and you want environment variables to be loaded from PROJECTNAME_VARNAME

"""This defines which environment variable global prefix dynaconf will load
That means that `export PROJECTNAME_FOO=1` will be loaded to `duanconf.settings.FOO
On command line it is possible to check it with `dynaconf list -k foo`"""

"""By default it is DYNACONF_ENV, this is the envvar used to switch from development to production
but with this settings your users can do `export PROJECT_ENV=production` (new in 2.0.0)"""

"""This defines which path dynaconf will look to load config files
example: export PROJECTNAME_SETTINGS=/path/to/settings.toml and the format can be
.ini, .json, .yaml or .toml


    export PROJECTNAME_SETTINGS=settings.toml
    FOO = 1
    FOO = 2
    FOO = 3


    export PROJECTNAME_SETTINGS=settings.yaml
      foo: 1
      foo: 2
      foo: 3

It is also possible to pass a list of files::

    export PROJECTNAME_SETTINGS=settings.toml,other_settings.yaml,another.json

The variables will be cascaded in the defined order (last wins the precedence)
The environment variables wins precedence over all!

# load dynaconf
settings = LazySettings(

Then the working environment can now be switched using export PROJECTNAME_ENV=production