Skip to main content

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

Project description

envu

This package provides a convenient way to load environment variables from a .env file, 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
Traceback (most recent call last):
 ...
AttributeError: Environment variable 'UNDEFINED_VARIABLE' not found.

>>> hasattr(env, "KEY")  # Check if an environment variable exists
True

>>> getattr(env, "MY_VARIABLE", "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 = getattr(env, "DEBUG", 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": getattr(env, "DJANGO_DB_PORT", 5432),
        "HOST": getattr(env, "DJANGO_DB_HOST", "localhost"),
    }
}


EMAIL_HOST_PASSWORD = env.EMAIL_HOST_PASSWORD
EMAIL_HOST_USER = env.EMAIL_HOST_USER
EMAIL_PORT = getattr(env, "EMAIL_PORT", 25)
EMAIL_HOST = getattr(env, "EMAIL_HOST", "localhost")
EMAIL_USE_TLS = getattr(env, "EMAIL_USE_TLS", 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.0.0.tar.gz (4.0 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.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: envu-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.1 CPython/3.11.3 Windows/10

File hashes

Hashes for envu-1.0.0.tar.gz
Algorithm Hash digest
SHA256 26974b1747aa7d3d79d7ab0009584fe4f159d94cb4878b87a6761b9c4e5726ea
MD5 f9d28bf7ffde870633c275665d9f536a
BLAKE2b-256 8f836ee2a836424c0bd0b16efb52c122162ed305802725a78af0e412feb503fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: envu-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.1 CPython/3.11.3 Windows/10

File hashes

Hashes for envu-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27524e68f3e7e3cce4048adfbb7f046896cbb6940c69890222c70ba097ccf7c2
MD5 612820244808e6d9bffd5b9fdf5897a6
BLAKE2b-256 c8f92468020ad99b73ca6e8721cce0c3151e41b5658ece9cc48f8b46148ccdef

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