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", expected_type=bool, default=False)
TIMEOUT = env("TIMEOUT", expected_type=int, default=300)

Installation

Simply install using pip by running

pip install easy_env_var

For Python 3.7 you will also need typed-ast which should automatically be installed. For Python 3.8 and above it is not needed.

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.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

easy_env_var-1.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: easy_env_var-1.2.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for easy_env_var-1.2.0.tar.gz
Algorithm Hash digest
SHA256 9b5d23b162e67404729544262af9c8270d0fe0ed5831b3c82485eb4b9915b04c
MD5 e055c7cfcd4a085b6dd4522a937976ed
BLAKE2b-256 4384d162a6e85ec0ed4384d0fe5da04004f0eb6ea218789248f33db05ad9eb7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for easy_env_var-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9921ef5d7756adb10f09b097acb13ee05be4ec7ddb8e476395614372a2df0f90
MD5 569ec3c5e309eab75cc7131af55cb815
BLAKE2b-256 33c54d943e208f7eda4da5b5fc3ce4837c45c8603eb1a74efbb72d15fa02face

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