Skip to main content

Load configuration variables from a file or environment

Project description

Define configuration variables and load them from environment or JSON/YAML file. Also generates initial configuration files and documentation for your defined configuration.


pip install goodconf or pip install goodconf[yaml] if parsing/generating YAML files is required.



# define a configuration
import base64
import os

from goodconf import GoodConf, Value

config = GoodConf(description="Configuration for My App")
    Value('DEBUG', default=False, help="Toggle debugging."),
    Value('DATABASE_URL', default='postgres://localhost:5432/mydb',
          help="Database connection."),
          initial=lambda: base64.b64encode(os.urandom(60)).decode(),
          help="Used for cryptographic signing. "

# load a configuration

# access values as attributes on the GoodConf instance

# generate an initial config file from the definition

# generate documentation for a configuration


The GoodConf object can be initialized with the following keyword args:

  • description A plain-text description used as a header when generating a configuration file.
  • file_env_var The name of an environment variable which can be used for the name of the configuration file to load.
  • default_files If no file is passed to the load method, try to load a configuration from these files in order.


The define_values method of GoodConf takes a list of Value instances. They can be initialized with the following keyword args:

  • key Name of the value used in file or environment variable.
  • default Default value if none is provided.
  • required Loading a config will fail if a value is not provided. Defaults to True if no default is provided otherwise False.
  • initial Initial value to use when generating a config
  • cast_as Python type to cast variable as. Defaults to type of default (if provided) or str.
  • help Plain-text description of the value.

Django Usage

A helper is provided which monkey-patches Django’s management commands to accept a --config argument. Replace your with the following:

import sys
from goodconf.contrib.django import execute_from_command_line_with_config
# Define your GoodConf in `myproject/`
from myproject import config

if __name__ == '__main__':
    execute_from_command_line_with_config(config, sys.argv)


I took inspiration from logan (used by Sentry) and derpconf (used by Thumbor). Both, however used Python files for configuration. I wanted a safer format and one that was easier to serialize data into from a configuration management system.

Environment Variables

I don’t like working with environment variables. First, there are potential security issues:

  1. Accidental leaks via logging or error reporting services.
  2. Child process inheritance (see ImageTragick for an idea why this could be bad).

Second, in practice on deployment environments, environment variables end up getting written to a number of files (cron, bash profile, service definitions, web server config, etc.). Not only is it cumbersome, but also increases the possibility of leaks via incorrect file permissions.

I prefer a single structured file which is explicitly read by the application. I also want it to be easy to run my applications on services like Heroku where environment variables are the preferred configuration method.

This module let’s me do things the way I prefer in environments I control, but still run them with environment variables on environments I don’t control with minimal fuss.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for goodconf, version 0.8.0
Filename, size File type Python version Upload date Hashes
Filename, size goodconf-0.8.0-py2.py3-none-any.whl (11.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size goodconf-0.8.0.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page