Skip to main content

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.

Metadata

Release files for importenv 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for importenv 1.0.0
File Size Uploaded Provenance Actions
importenv-1.0.0.tar.gz 3.5 kB Without provenance Details for importenv-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for importenv 1.0.0
File Interpreter ABI Platform Actions
importenv-1.0.0-py3-none-any.whl Python 3 none any Details for importenv-1.0.0-py3-none-any.whl

Total release size: 7.4 kB

Release files / importenv-1.0.0.tar.gz

Download URL importenv-1.0.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
44b9c46b944e857d9dfaf044e1564a693e9ddc973cbe460f93e2a0e1175b5d03
BLAKE2b-256 checksum
How to use checksums
52ee29aa37537d50b97287febaf0b352c43e080262102591d0ebc6546a43559f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.3 Linux/6.11.0-21-generic

Release files / importenv-1.0.0-py3-none-any.whl

Download URL importenv-1.0.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0fdbe912dee20fbfe6767cd94cf0c156484551f63fd326bbc822585a78221f02
BLAKE2b-256 checksum
How to use checksums
7cd2fe5d601efd11461bbd5faf4f1e432c27182faba83993ee1a1fd7e449badf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.1 CPython/3.12.3 Linux/6.11.0-21-generic

Security for importenv 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page