Skip to main content

The simplest possible syntax for loading environment variables from the .env file and system environment variables.

Project description

envu

This package provides a convenient way to load environment variables from the .env file and system environment variables, automatically parsing them into appropriate Python types (bool, int, float, or str). It also allows you to access the loaded variables as attributes.

Features:

  • Parses .env file values into their correct types.
  • Supports boolean, integer, float, and string conversions.
  • Skips empty lines and comments.
  • Raises an error for malformed .env lines or missing variables.
  • Provides a __dir__ method for inspecting available keys.

Installation

pip install envu

Usage

>>> import env  # not envu

>>> env.KEY  # Access environment variables as attributes
'Value'

>>> env.DEBUG  # Boolean Variable
False

>>> env.SECRET_KEY  # String Variable
'fake-secret-key'

>>> env.POSTGRESQL_PORT  # Integer Variable
5432

>>> env.SLEEP_TIME  # Float Variable
1.5

>>> env.ALLOWED_HOSTS.split(",")  # Convert to a list
["localhost", "127.0.0.1", "example.com"]

>>> env.UNDEFINED_VARIABLE  # Undefined Variable
None

>>> env.MY_VARIABLE or "default_value"  # Get the variable with a default fallback
"default_value"

Example: How do I use it with Django?

For a typical Django project, the .env file is placed like this:

my_project/
├── .env                 # Your environment variables
├── manage.py            # Django project entry point
├── my_project/          # Main application folder   ├── __init__.py
│   ├── settings.py      # Django settings file where .env is loaded   ├── urls.py
│   └── wsgi.py
└── apps/
    ├── app1/
    └── app2/

An example of Django settings

import env
from unipath import Path

BASE_DIR = Path(__file__).parent


SECRET_KEY = env.SECRET_KEY
DEBUG = env.DEBUG or False
ALLOWED_HOSTS = env.ALLOWED_HOSTS.split(",")


DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": env.DJANGO_DB_NAME,
        "USER": env.DJANGO_DB_USERNAME,
        "PASSWORD": env.DJANGO_DB_PASSWORD,
        "PORT": env.DJANGO_DB_PORT or 5432,
        "HOST": env.DJANGO_DB_HOST or "localhost",
    }
}


EMAIL_HOST_PASSWORD = env.EMAIL_HOST_PASSWORD
EMAIL_HOST_USER = env.EMAIL_HOST_USER
EMAIL_PORT = env.EMAIL_PORT or 25
EMAIL_HOST = env.EMAIL_HOST or "localhost"
EMAIL_USE_TLS = env.EMAIL_USE_TLS or False

# ...

Running Tests

To run the tests, make sure you have pytest installed. You can install it using requirements.txt:

pip install -r requirements.txt

Then run:

pytest

This command will discover and execute all test cases in the project. For more advanced options, you can refer to the pytest documentation.

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

envu-1.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

envu-1.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: envu-1.2.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-52-generic

File hashes

Hashes for envu-1.2.0.tar.gz
Algorithm Hash digest
SHA256 e64758853fc8d276c2025116a130b4250cedb8520910a1b23aea6f9f0f3d8668
MD5 872960db069271e14a32bfb73439e50e
BLAKE2b-256 0c4144ad020a2f5314e650aab0a2c967d6d64da3df47f31370114c259ebe7735

See more details on using hashes here.

File details

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

File metadata

  • Download URL: envu-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-52-generic

File hashes

Hashes for envu-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a544865798e7bf58f28b754fff3f0b44eaf7f97908d91cbae279b32f89047072
MD5 dd71607147bcc20f08c1235dc95eab8f
BLAKE2b-256 249dcf30c79058ea50d9c83e888c166538d88f02eee722368663a1b53b8faed6

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