Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Parse configuration values from files, the environment, and elsewhere all in one place.

Project description

Here is a full working example of using easy_config. First, write your configuration class:

# config.py
from easy_config import EasyConfig

class MyProgramConfig(EasyConfig):
   FILES = ['myprogram.ini']
   NAME = 'MyProgram'  # the name for the .ini file section and the namespace prefix for environment variables

   # define the options like you would a dataclass
   number: int
   name: str
   check_bounds: bool = True  # options with defaults must all come after non-default options

A sample configuration file:

# myprogram.ini
[MyProgram]
# section name matches `NAME` from the configuration class
number = 3

And a sample program to illustrate the usage:

# test_config.py
import sys

from config import MyProgramConfig

print(MyProgramConfig.load(name=sys.argv[1])

Running this program with various options:

$ python test_config.py Scott
MyProgramConfig(number=3, name='Scott', check_bounds=True)

$ env MYPROGRAM_CHECK_BOUNDS=False python test_config.py Scott
# environment variable names are the all-uppercase transformation of the NAME concatenated with the option name and an underscore
MyProgramConfig(number=3, name='Scott', check_bounds=False)

$ env MYPROGRAM_NUMBER=10 MYPROGRAM_NAME=Charlie python test_config.py Scott
MyProgramConfig(number=10, name='Scott', check_bounds=True)

As you can see, values are taken in precedence, with arguments passed to load overriding values from the environment which, in turn, override values from configuration files.

Once you have the MyProgramConfig instance, you can use it just like any dataclass.

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 easy-config, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size easy_config-0.2.0-py36-none-any.whl (7.2 kB) File type Wheel Python version py36 Upload date Hashes View
Filename, size easy_config-0.2.0.tar.gz (11.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page