Skip to main content

An extended and convenient os.environ interface

Project description


env exTENDED


dotenv .env aware environment variable handling with typing features

Overview

This is a refactoring of django-settings-env with Django specific functionality stripped out, and so implements all of the smart environment handling suitable for use outside of Django.

This module provides a convenient interface for handling the environment, and therefore configuration of any application using 12factor.net principals removing many environment specific variables and security sensitive information from application code.

This module provides some features not supported by other dotenv handlers (python-dotenv, etc.) including expansion of template variables which is very useful for DRY.

An Env instance delivers a lot of functionality by providing a type-smart front-end to os.environ, with support for most - if not all - os.environ functionality.

from envex import env

assert env['HOME'] ==  '/Users/davidn'
env['TESTING'] = 'This is a test'
assert env['TESTING'] == 'This is a test'

import os
assert os.environ['TESTING'] == 'This is a test'

assert env.get('UNSET_VAR') is None
env.set('UNSET_VAR', 'this is now set')
assert env.get('UNSET_VAR') is not None
env.setdefault('UNSET_VAR', 'and this is a default value but only if not set')
assert env.get('UNSET_VAR') == 'this is now set'
del env['UNSET_VAR']
assert env.get('UNSET_VAR') is None

An Env instance can also read a .env (default name) file and update the application environment accordingly.

It can read this either from __init__ or via the method read_env().

  • Override the base name of the dot env file, use the DOTENV environment variable.

  • Other kwargs that can be passed to Env.__init__

    • env_file (str): base name of the env file, os.environ["DOTENV"] by default, or .env
    • search_path (str or list): a single path or list of paths to search for the env file
    • overwrite (bool): overwrite already set values read from .env, default is to only set if not currently set
    • parents (bool): search (or not) parents of dirs in the search_path
    • update (bool): update os.environ if true (default) otherwise pool changes internally
    • environ (env): pass the environment to update, default is os.environ
  • Env() also takes an additional kwarg, readenv (default False) which instructs it to read dotenv files

Some type-smart functions act as an alternative to Env.get and having to parse the result:

from envex import env

env['AN_INTEGER_VALUE'] = 2875083
assert env.get('AN_INTEGER_VALUE') == '2875083'
assert env.int('AN_INTEGER_VALUE') == 2875083

env['A_TRUE_VALUE'] = True
assert env.get('A_TRUE_VALUE') == 'True'
assert env.bool('A_TRUE_VALUE') is True

env['A_FALSE_VALUE'] = 0
assert env.get('A_FALSE_VALUE') == '0'
assert env.int('A_FALSE_VALUE') == 0
assert env.bool('A_FALSE_VALUE') is False

env['A_FLOAT_VALUE'] = 287.5083
assert env.get('A_FLOAT_VALUE') == '287.5083'
assert env.float('A_FLOAT_VALUE') == 287.5083

env['A_LIST_VALUE'] = '1,"two",3,"four"'
assert env.get('A_LIST_VALUE') == '1,"two",3,"four"'
assert env.list('A_LIST_VALUE') == ['1', 'two', '3', 'four']

Note that environment variables are always stored as strings. This is enforced by the underlying os.environ, but also true of any provided environment, using the MutableMapping[str, str] contract.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envex-1.5.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

envex-1.5.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file envex-1.5.1.tar.gz.

File metadata

  • Download URL: envex-1.5.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for envex-1.5.1.tar.gz
Algorithm Hash digest
SHA256 4bbecb4b52f5ed17a8c9ab31d786605c3f60bbd3757b1cedd8a83c9c5fffb790
MD5 87c65e8439bd649629607c9e53dc3fbd
BLAKE2b-256 a73241ac711f3dbc79a2ffeed12e944c54c0aab6fba3252af10ff775f2263530

See more details on using hashes here.

File details

Details for the file envex-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: envex-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for envex-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 012e9206f9fbc26b0da31690cdc92dad06384a44eb40787538c9ee4559955503
MD5 8710b3072b5d4b1f8db87c346b3562ce
BLAKE2b-256 98b36f92735418658eeba0207d24725ddf75f1c6a8fd17b0b4ef8239ffc3c428

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page