Stop writing settings files

Stop writing settings files


Bruno Renié

May 15, 2013


  1. 3.

    local_settings # # settings goes here, and at the

    end… try: from local_settings import * except ImportError: pass
  2. 5.

    # settings/ INSTALLED_APPS = … # settings/ from .base import

    * # override here with # production-specific settings Multiple settings files $ python shell --settings=settings.production
  3. 7.

    — " " … strict separation of config from

    code. Config varies substantially across deploys,code does not.
  4. 8.

    Expose your configuration as environment variables Derive your settings from

    the environment DEBUG = bool(os.environ.get('DEBUG', False)) Use one settings file
  5. 11.

    daemontools' envdir path/to/env/ DATABASE_URL SENTRY_DSN … $ envdir path/to/env <child

    program> $ envdir path/to/env <command> Sane defaults for development kept under version control
  6. 12.

    envdir in ur if __name__ == "__main__": if 'test'

    in sys.argv: env_dir = os.path.join('tests', 'envdir') else: env_dir = 'envdir' env_vars = glob.glob(os.path.join(env_dir, '*')) for env_var in env_vars: with open(env_var, 'r') as env_var_file: os.environ.setdefault(env_var.split(os.sep)[-1], from import execute_from_command_line execute_from_command_line(sys.argv)
  7. 13.

    One settings files, less moving parts Configuration as environment variables

    Production: set them with daemontools, circus, supervisor… Development: use envdir and/or a custom Your PaaS already supports them Easy to setup with Salt/Puppet/Chef Your sysadmin will thank you