Skip to main content

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

Project description

This library extends the standard library’s version of the same function, allowing you to coerce the return value into a boolean or integer.

It contains a single useful function - getenv. 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
"0"
>>> boo(val)
True

env_utils.getenv will coerce the value into the type you require:

>>> env_utils.getenv('foo')
"0"
>>> env_utils.getenv('foo', coerce=coerce_bool)
False
>>> env_utils.getenv('foo', coerce=coerce_int)
0

Installation

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

$ pip install python_env_utils

Tests

There are tests in the package - they can be run using unittest:

$ python -m unittest env_utils.tests

Project details


Supported by

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