Usage¶
Read dotenv file¶
from yaenv.core import Env
env = Env('.env')
Access variables¶
# raises EnvError if missing
password = env['PASSWORD']
# returns 'user' if missing
username = env.get('USERNAME', default='user')
Set variables¶
env['EMAIL'] = 'user@example.com'
Unset variables¶
del env['EMAIL']
Interpolation¶
# POSIX variable expansion is supported
DOMAIN=example.com
EMAIL=user@${DOMAIN}
Type casting¶
env.bool('BOOL_VAR', default=True)
env.int('INT_VAR', default=5)
env.float('FLOAT_VAR', default=0.5)
env.list('LIST_VAR', default=[], separator=':')
Secret key¶
# Generate a cryptographically secure
# secret key if not already present
secret = env.secret('SECRET_KEY')
And more¶
# Get the path of the dotenv file
env.envfile
# Get all the variables in the dotenv file
env.vars
# Check if a variable is in the file
'EMAIL' in env
# Get the number of variables in the file
len(env)
# Iterate over the variables in the file
for key, val in env:
print(f'{key}: {val}')
# Add the variables to os.environ
env.setenv()
# Access os.environ
env.ENV