Skip to main content
https://travis-ci.org/yunojuno/python-env-utils.svg?branch=master https://badge.fury.io/py/python-env-utils.svg https://codecov.io/gh/yunojuno/python-env-utils/branch/master/graph/badge.svg

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
"0"
>>> bool(val)
True

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')
"0"
>>> env_utils.get_bool('FOO')
False
>>> env_utils.get_int('FOO')
0

# 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')
datetime.date(2016, 11, 23)

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

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

Installation

The library is available at pypi as ‘python-env-utils’, and can installed using pip:

$ pip install python-env-utils

Tests

The tests can be run using tox:

$ tox

Metadata

Release files for python-env-utils 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-env-utils 0.4.1
File Size Uploaded
python-env-utils-0.4.1.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-env-utils 0.4.1
File Interpreter ABI Platform
python_env_utils-0.4.1-py2-none-any.whl Python 2 none any Details

Total release size: 14.5 kB

Release files / python-env-utils-0.4.1.tar.gz

Download URL python-env-utils-0.4.1.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6357d9ae024e5039158ce337bafeca662453f41cd7789a4517217c1a9093ce57
BLAKE2b-256 checksum
How to use checksums
5796c49c675b9a8cfb79b7377bb5e357feafb810dd2831201cde4e499c0a5e52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / python_env_utils-0.4.1-py2-none-any.whl

Download URL python_env_utils-0.4.1-py2-none-any.whl
Size 8.7 kB
Tags Python 2
SHA-256 checksum
How to use checksums
7706f7a05af68aefba4b142ae8de970016bfee98c03d3de93d5b049fd12a4688
BLAKE2b-256 checksum
How to use checksums
d516c39469945067a0b675f2cc0ae6b8e37f68e77d846ae216d05a0f7e7c9323
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page