2

I'd like to have a configuration file with some default values and also have the ability to override specific values via environment variables. I saw various examples of project configuration, e.g. this, but couldn't find the answer.
I know how to do it manually, i.e. check

if os.getenv('my_var')

exists, but I'm looking for a built in solution.

2 Answers 2

1

You are may looking for simple-settings library

$ pip install simple-settings

>>> from simple_settings import settings
>>> print(settings.FOO)
'some value in foo'

Values in simple-settings can be overriden by env variables.

Sign up to request clarification or add additional context in comments.

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Edited! I've just added a simple example on how to use simple settings :)
0

I don't know of any built-in solution for overriding options with environment variables, but you can probably work out some naming convention to make it pretty easy.

I usually have three places to change settings in my programs:

  1. The source code holds generic defaults that will make sense for most developers, with an exception for security settings that should be secure by default.
  2. Environment variables can override the source code defaults.
  3. Command-line arguments can override the environment variables. Just be careful not to put passwords and other sensitive data on the command line, because process command lines can usually be seen by other users on the same system.

It sounds like you want to introduce another configuration file between level 1 and 2.

Here's what my code usually looks like:

def parse_args(argv=None):
    parser = ArgumentParser(description='What my program does...',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '--server',
        default=os.environ.get('MYAPP_SERVER', 'http://localhost:8000'),
        help='server to send data to')
    parser.add_argument(
        '--user',
        default=os.environ.get('MYAPP_USER', 'alex'),
        help='user name for server')
    parser.add_argument(
        '--password',
        default=SUPPRESS,
        help='password for server (default not shown)')

    args = parser.parse_args(argv)
    if not hasattr(args, 'password'):
        args.password = os.environ.get('MYAPP_PASSWORD', 'alex')
    return args

If you want to look in some config file, it might look something like this:

def parse_args(argv=None):
    parser = ArgumentParser(description='What my program does...',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '--server',
        default=get_default('server', 'http://localhost:8000'),
        help='server to send data to')
    parser.add_argument(
        '--user',
        default=get_default('user', 'alex'),
        help='user name for server')
    parser.add_argument(
        '--password',
        default=SUPPRESS,
        help='password for server (default not shown)')

    args = parser.parse_args(argv)
    if not hasattr(args, 'password'):
        args.password = get_default('password', 'alex')
    return args


def get_default(name, value):
    file_value = read_config_file(name)
    if file_value is not None:
        value = file_value
    return os.environ.get(f'MYAPP_{name.upper()}', value)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.