Published in 13:39 of 02/14/2016 by Bruno Rocha
Bruno Rocha There is more to life than increasing its speed!

Published in 13:39 of 02/14/2016


Dynaconf - Let your settings to be Dynamic


dynaconf - The dynamic configurator for your Python Project

MIT License PyPI downloads Travis CI Coverage Status Code Health

dynaconf is an OSM (Object Settings Mapper) it can read settings variables from a set of different data stores such as python settings files, environment variables, redis, memcached, ini files, json files, yaml files and you can customize dynaconf loaders to read from wherever you want. (maybe you really want to read from xml files ughh?)


What is Dynaconf?

Dynaconf is a common point of access to settings variables, you import only one object in your project and from that object you can access settings variables from Python settings file, from environment variables, from parsed yaml, ini, json or xml files, from datastores as Redis and MongoDB or from wherever your need if you write a simple dynaconf loader.

How it works

Install it

pip install dynaconf

Use it

from dynaconf import settings
print settings.SOME_VARIABLE
print settings.get('SOME_VARIABLE')

By default Dynaconf will try to use a file called on the root of your project, if you place that file there all upper case variables will be read

You can also replace the file exporting an environment variable pointing to the module or location for the settings file.

# using module name
export DYNACONF_SETTINGS=myproject.production_settings
# or using location path
export DYNACONF_SETTINGS=/etc/myprogram/

Doing that when you use from dynaconf import settings the variables will be read from that file.

So how it is Dynamic?

Now think you have your program done and you want to deploy to a certain infrastructure for testing or maybe different deployment, you don't need to rewrite the settings file. Just export some variables to your environment.


Now in your project you can do:

from dynaconf import settings
print settings.MYSQL_HOST

The default prefix for exported envvars is by default DYNACONF_ but you also can change it if needed.

But what if I have some typed values to export?

You can also define type casting when exporting and those types will be used to parse the values.

export DYNACONF_NUMBER='@int 123'
export DYNACONF_FLOAT='@float 12.2'
export DYNACONF_FLAG='@bool yes'
export DYNACONF_FLAG2='@bool disabled'
export DYNACONF_LIST='@json [1, 2, 3, 4]'
export DYNACONF_DICT='@json {"name": "Bruno"}'

Now you can read all those values from your project and it will be loaded with correct type casting.

from dynaconf import settings




print settings.FLAG2 == False

print settings.LIST[1]

print settings.DICT['name']

Nice! But I don't want to use envvars because I use autoscaling and I want my machines to share a settings environment how to do it?


Go to your settings file (default and put

# connection
    'host': 'localhost',
    'port': 6379,
    'db': 0

# and loader

Now you can store settings variables directly in Redis using a hash named by default DYNACONF_DYNACONF

If you don't want want to write directly you can use the Redis writer helper in a python REPL. (ipython as example)

from dynaconf.utils import redis_writer
from dynaconf import settings

redis_writer.write(settings, name='Bruno', mysql_host='localhost', MYSQL_PORT=1234)

And the above will be store in Redis as a hash int the form.

    PORT='@int 1234'

And of course you can now read those variables in the project, all the casting wildcards also works on Redis but if you want to skip type casting, write as string intead of PORT=1234 use PORT='1234' as redis stores everything as string anyway.

There is more

Dynaconf has support for using different namespaces in the same project, you can also write your own loaders, you can find more information on the repository


All contributions are very welcome!!


Dynaconf was inspired by Flask app config and also by Django settings module.

  • Microservices with Python, RabbitMQ and Nameko in python · 17:05 of 03/04/2016
  • ESEngine - Elasticsearch Object Doctype Mapper for Python in python · 12:56 of 02/25/2016
  • Flasgger - API playground with Flask and Swagger UI in flask · 10:36 of 02/23/2016

  • comments powered by Disqus Go Top