Skip to content
A simple library for loading configurations easily in Python, inspired by `flask.config`.
Python Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
easyconfig
tests
.gitignore
CHANGELOG.md
Makefile
README.md
requirements.txt
setup.cfg
setup.py
tox.ini

README.md

EasyConfig

A simple library for loading configurations easily in Python, inspired by flask.config.

Installation

Install EasyConfig with pip:

$ pip install python-easyconfig

Install development version from GitHub:

$ git clone https://github.com/RussellLuo/easyconfig.git
$ cd easyconfig
$ python setup.py install

Getting Started

1. load default configurations from a dictionary (or an object)

    # from a dictionary
    from easyconfig import Config
    config = Config({'DEBUG': True})
    assert config.DEBUG == True

    # from an object
    from easyconfig import Config
    from yourapplication import default_config
    config = Config(default_config)
    assert config.DEBUG == True

2. load/update configurations from a dictionary

    from easyconfig import Config
    config = Config()
    config.load({'PORT': 5000})

3. load/update configurations from an object

    from easyconfig import Config
    from yourapplication import default_config
    config = Config()
    config.load(default_config)

4. load configurations from environment variables

    import os
    os.environ.setdefault('SECRET_KEY', '123***456')

    from easyconfig import Config
    config = Config(datasrc=os.environ)
    assert config.SECRET_KEY == '123***456'

5. load/update configurations from a string pointing to an object

    from easyconfig import Config, str_object
    config = Config()
    config.load(str_object('yourapplication.default_config'))
    assert config.SECRET_KEY == '123***456'

6. load/update configurations from an environment variable pointing to an object

    import os
    os.environ.setdefault('EASY_CONFIG', 'yourapplication.default_config')

    from easyconfig import Config, str_object
    config = Config()
    config.load(str_object('EASY_CONFIG', is_envvar=True))
    assert config.SECRET_KEY == '123***456'

7. load/update configurations from a JSON file

    from easyconfig import Config, json_mapping
    config = Config()
    config.load(json_mapping('/data/configs/default_config.json'))
    assert config.SECRET_KEY == '123***456'

8. load/update configurations from an environment variable pointing to a JSON file

    import os
    os.environ.setdefault('EASY_CONFIG', '/data/configs/default_config.json')

    from easyconfig import Config, json_mapping
    config = Config()
    config.load(json_mapping('EASY_CONFIG', is_envvar=True))
    assert config.SECRET_KEY == '123***456'

9. load/update configurations from a YAML file

    from easyconfig import Config, yaml_mapping
    config = Config()
    config.load(yaml_mapping('/data/configs/default_config.yml'))
    assert config.SECRET_KEY == '123***456'

10. load/update configurations from an environment variable pointing to a YAML file

    import os
    os.environ.setdefault('EASY_CONFIG', '/data/configs/default_config.yml')

    from easyconfig import Config, yaml_mapping
    config = Config()
    config.load(yaml_mapping('EASY_CONFIG', is_envvar=True))
    assert config.SECRET_KEY == '123***456'
You can’t perform that action at this time.