Configuration Managers

Creating a ConfigManager and specifying environments

First, you define a everett.manager.ConfigManager which specifies the environments you want to pull configuration from.

Then you can use the everett.manager.ConfigManager to look up configuration keys. The everett.manager.ConfigManager will go through the environments in order to find a value for the configuration key. Once it finds a value, it runs it through the parser and returns the parsed value.

There are a few ways to create a everett.manager.ConfigManager.

The easiest is to use everett.manager.ConfigManager.basic_config().

For example:

# myserver.py

"""
Minimal example showing how to use configuration for a web app.
"""

from everett.manager import ConfigManager

config = ConfigManager.basic_config(
    doc="Check https://example.com/configuration for documentation."
)

host = config("host", default="localhost")
port = config("port", default="8000", parser=int)
debug_mode = config(
    "debug",
    default="False",
    parser=bool,
    doc="Set to True for debugmode; False for regular mode",
)

print(f"host: {host}")
print(f"port: {port}")
print(f"debug_mode: {debug_mode}")

That creates a everett.manager.ConfigManager that looks up configuration keys in these environments:

  1. the process environment

  2. the specified env file which defaults to .env

That works for most cases.

You can create your own everett.manager.ConfigManager and specify environments specific to your needs.

For example:

# myserver_with_environments.py

"""
Minimal example showing how to use configuration for a web app that pulls
configuration from specified environments.
"""

import os
from everett.ext.inifile import ConfigIniEnv
from everett.manager import ConfigManager, ConfigOSEnv, ConfigDictEnv

config = ConfigManager(
    [
        # Pull from the OS environment first
        ConfigOSEnv(),
        # Fall back to the file specified by the FOO_INI OS environment
        # variable if such file exists
        ConfigIniEnv(os.environ.get("FOO_INI")),
        # Fall back to this dict of defaults
        ConfigDictEnv({"FOO_VAR": "bar"}),
    ],
    doc="Check https://example.com/configuration for documentation.",
)

host = config("host", default="localhost")
port = config("port", default="8000", parser=int)
debug_mode = config(
    "debug",
    default="False",
    parser=bool,
    doc="Set to True for debugmode; False for regular mode",
)

print(f"host: {host}")
print(f"port: {port}")
print(f"debug_mode: {debug_mode}")

Specifying configuration documentation

When building a everett.manager.ConfigManager, you can specify documentation for configuration. It will get printed when there are configuration errors. This is a great place to put a link to configuration documentation.

For example:

# myserver.py

"""
Minimal example showing how to use configuration for a web app.
"""

from everett.manager import ConfigManager

config = ConfigManager.basic_config(
    doc="Check https://example.com/configuration for documentation."
)

host = config("host", default="localhost")
port = config("port", default="8000", parser=int)
debug_mode = config(
    "debug",
    default="False",
    parser=bool,
    doc="Set to True for debugmode; False for regular mode",
)

print(f"host: {host}")
print(f"port: {port}")
print(f"debug_mode: {debug_mode}")

Let’s set DEBUG wrong and see what it tells us:

$ python myserver.py
host: localhost
port: 8000
debug_mode: False

$ DEBUG=foo python myserver.py
<traceback>
everett.InvalidValueError: ValueError: 'foo' is not a valid bool value
DEBUG requires a value parseable by everett.manager.parse_bool
DEBUG docs: Set to True for debugmode; False for regular mode
Project docs: Check https://example.com/configuration for documentation.

Here, we see the documentation for the DEBUG option, the documentation from the ConfigManager, and the specific Python exception information.