Skip to main content

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

Project description

importenv

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 importenv

Usage

>>> import env

>>> 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

importenv-1.0.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

importenv-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: importenv-1.0.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.11.0-21-generic

File hashes

Hashes for importenv-1.0.0.tar.gz
Algorithm Hash digest
SHA256 44b9c46b944e857d9dfaf044e1564a693e9ddc973cbe460f93e2a0e1175b5d03
MD5 8daf7f90492a2534509fa6ba8798fddb
BLAKE2b-256 52ee29aa37537d50b97287febaf0b352c43e080262102591d0ebc6546a43559f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: importenv-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.11.0-21-generic

File hashes

Hashes for importenv-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0fdbe912dee20fbfe6767cd94cf0c156484551f63fd326bbc822585a78221f02
MD5 44865ccd81c227547bb559d7788ca35b
BLAKE2b-256 7cd2fe5d601efd11461bbd5faf4f1e432c27182faba83993ee1a1fd7e449badf

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