dotenv-config

Simple dotenv loader with the possibility of casting types


Keywords
dotenv, config, env, types, cast, configuration-management, python-library, python3
License
MIT
Install
pip install dotenv-config==0.1.4

Documentation

Dotenv config

Simple .env config loader with type casting.

Build Status Coverage Status Python versions PyPi

Installation

pip3 install dotenv-config  # or pip if python3 is your default interpreter

Usage

All you need is instantiate the Config class. As an optional argument you can specify the path to your config file, by default this is .env

from dotenv_config import Config

config = Config('.env-test')

To read the configuration, call the Config instance. By default the returned value is a string.

some_str_option = config('SOME_OPTION_FROM_YOUR_ENV_FILE_OR_ENVIRONMENT')  # str

To cast a type, any callable object can be passed as second argument or conversion. As an argument, it must accept the value read from the config and return the result of type casting. For example, if you have SOME_INT=123 in your .env and need to load it as int, call config like this:

int_option = config('SOME_INT', int)
# or
int_option = config('SOME_INT', conversion=int)

You can perform a cast to boolean if the option is specified in the config as 0 or 1. For example, if you have a TRUE_OPTION=1 and FALSE_OPTION=0:

true_option = config('TRUE_OPTION', bool)  # True
false_option = config('FALSE_OPTION', bool)  # false

Also, you can specify a default value for situations when the requested option may not be:

some_value = config('SOME_VALUE', default='my english is bad')

If the requested option does not exist and the default value is not specified, an ConfigValueNotFound exception will be raised.