Skip to main content

Utility functions to make it easier to work with os.environ

Project description

env utils

This library extends the standard library’s getenv function, allowing you to coerce the return value into a type.

And that’s it.

It’s been released as a library because every project we have includes the same requirements - read in environment variables, coerce them into the correct type.

The problem is that environment variables are always stored as strings, but Python will evaluate any string (even “”) as True if cast to a boolean. This is almost never the desired behaviour. If you set an environment variable to “”, “0” or “False”, you want it to be False.

>>> os.environ['foo'] = "0"
>>> val = os.getenv('foo')
>>> val
>>> bool(val)

env_utils.get_env will coerce the value into the type you require. The package contains basic helper functions that coerce booleans, integers, decimals, floats, dates, lists and dictionaries.

# FOO=0
>>> env_utils.get_env('FOO')
>>> env_utils.get_bool('FOO')
>>> env_utils.get_int('FOO')

# FOO=foo,bar
>>> env_utils.get_list('FOO', separator=',')
['foo', 'bar']

# FOO='{"foo": true}'
>>> env_utils.get_dict('FOO')
{'foo': True}

# FOO=2016-11-23
>>> env_utils.get_date('FOO'), 11, 23)

You can supply any function you like to coerce the value, e.g.:

>>> import os
>>> os.getenv('FOO_NAME')
>>> class Foo(object):
...     def __init__(self, name):
... = name
>>> coerce = lambda x: Foo(x)
>>> import env_utils
>>> foo = env_utils.get_env('FOO_NAME', coerce=coerce)
>>> 'bob'


The library is available at pypi as ‘env_utils’, and can installed using pip:

$ pip install env_utils


The tests can be run using tox:

$ tox

Project details

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
env_utils-1.2.0.macosx-10.12-intel.tar.gz (4.7 kB) Copy SHA256 hash SHA256 Dumb Binary any Nov 24, 2016
env_utils-1.2.0-py2-none-any.whl (5.4 kB) Copy SHA256 hash SHA256 Wheel 2.7 Nov 24, 2016
env_utils-1.2.0.tar.gz (4.4 kB) Copy SHA256 hash SHA256 Source None Nov 24, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page