Skip to main content

Simple util to get environment variables in the right data type.

Project description

Easy Environment Variables

An easier way to use environment variables with the correct typecasting in Python.

pipeline status coverage report PyPI - Python Version Code style: black Imports: isort License: MIT

Since environment variables can only be strings, it usually means we need loads of related variables or weird casting in our code. Without easy_env_var that would look something like this

CACHE_KEY=default
CACHE_BACKEND=redis_cache.RedisCache
CACHE_LOCATION=redis://localhost:6379/3
DEBUG=true
TIMEOUT=1000
import os

CACHES = {
    os.environ.get("CACHE_KEY", "default"): {
        "BACKEND": os.environ["CACHE_BACKEND"],
        "LOCATION": os.environ["CACHE_LOCATION"],
    }
}
DEBUG = os.environ.get("DEBUG", "false") == "false"
TIMEOUT = int(os.environ.get("TIMEOUT", "300"))

With easy_env_var it is as simple as

CACHES = {"default": {"BACKEND": redis_cache.RedisCache, "LOCATION": redis://localhost:6379/3}}
DEBUG=true
TIMEOUT=1000
from easy_env_var import env

CACHES = env("CACHE", expected_type=dict)
DEBUG = env("DEBUG", bool, default=False)
TIMEOUT = env("TIMEOUT", int, 300)

Installation

Simply install using pip by running

pip install easy_env_var

Usage

from easy_env_var import env
FOO = env("foo")  # get the environment variable named foo
BAR = env("bar", expected_type=float)  # cast to the correct data type
try:
    BAZ = env("baz")
except KeyError:
    BAZ = "Not Set"
# or simply pass a default
QUX = env("qux", default="The default value")

Supported data types

Environment variables can be parsed to the following data types:

  • str (this is the default type)
  • int
  • float
  • bool (works with case insensitive values of True & False)
  • Decimal
  • list
  • dict (the keys need to always be strings) Sets and tuples are not supported since using lists is good enough for most use cases.

Why easy_env_var?

We wanted to call it easy_env but that's an existing package.

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

easy_env_var-1.0.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_env_var-1.0.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file easy_env_var-1.0.0.tar.gz.

File metadata

  • Download URL: easy_env_var-1.0.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.1

File hashes

Hashes for easy_env_var-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f504cf40743a0653eb256cdc9661ef02bd040ce68e67028714a5e94d81e59d48
MD5 3ca3ac3fd3ec38752605cca1afe51ae9
BLAKE2b-256 f03433e38f6b9faa94b99ccc87b4dca0c979dccc46cdef5116ccb64241c0bb11

See more details on using hashes here.

File details

Details for the file easy_env_var-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: easy_env_var-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.1

File hashes

Hashes for easy_env_var-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19f0a4c8210bc1baf37ac930515b521c8ab2ad5149b913a08465e0c7d2d551b7
MD5 8554f49e916cfc66f70d7cef726e6052
BLAKE2b-256 402069173aadb0364229613021d0153ac17100450eb60d4fe89cc582f4677095

See more details on using hashes here.

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