Skip to main content

TOML powered .env file configuration and secrets

Project description

TOML-decouple

GitHub Workflow Status PyPI - Version Pypi - Python Version PyPI - Downloads License

TOML powered .env file configuration and secrets

Package available in PyPI.

Getting Started

Install the library

Using pip:

pip install toml-decouple

Using uv:

uv add toml-decouple

Basic Usage

# my_project/.env
SECRET_KEY = "S3cre7"
DEBUG = true
DB = postgresql://USER:PASSWORD@HOST:PORT/NAME
import dj_database_url
from toml_decouple import config

SECRET_KEY = config.SECRET_KEY
DEBUG = config("DEBUG", false)
DATABASES["default"] = config("DB", to=dj_database_url.parse)

Assuming this environment variable is set:

MY_PROJECT_DEBUG=true

Create your configuration dataclass and parse config and env into it:

from dataclasses import dataclass
from toml_decouple import TomlDecouple

@dataclass
class Config:
    SECRET_KEY: str = ""
    DEBUG: bool = False

config: Config = TomlDecouple().load_dataclass(Config)

You can now access the fields of your fully typed config Class that contains values from a TOML config file and the environment.

For example:

SECRET_KEY = config.SECRET_KEY
DEBUG = config("DEBUG", False)
print(SECRET_KEY)  # prints "S3cre7"
print(DEBUG)  # prints True
print(config.DB)  # raise AttributeError: 'Config' object has no attribute 'DB'

Configuration

To configure, you have to use TomlDecouple(**options).load(), like:

from toml_decouple import TomlDecouple

config = TomlDecouple(
    env_files=[".env.docker"],
    secrets=["/run/secrets"],  # this is the default
    initial={"DEBUG": True}
    prefix="BACKEND_",  # to use env variables BACKEND_ENV_VAR=value
).load()

Tests

Run tests from the project root with:

make test

Issues

Feel free to send issues or suggestions to https://github.com/blfpd/toml-decouple/issues.

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

toml_decouple-0.11.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

toml_decouple-0.11.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file toml_decouple-0.11.0.tar.gz.

File metadata

  • Download URL: toml_decouple-0.11.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for toml_decouple-0.11.0.tar.gz
Algorithm Hash digest
SHA256 1c177fcb1bf65929d1fb7b727e05c96bdd4dc6fbb897e44aec93c5551d6c7538
MD5 b19bc67496065a631fd3649e75a89c7e
BLAKE2b-256 ecbf2f0a72bf0ce23976a698a1915a6527349549ebccc7de0c51a445660f6d74

See more details on using hashes here.

Provenance

The following attestation bundles were made for toml_decouple-0.11.0.tar.gz:

Publisher: publish.yaml on blfpd/toml-decouple

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file toml_decouple-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: toml_decouple-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for toml_decouple-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 01d51ca07f5b71b085af2ac0bac84f1c19113566738aa7ebe2bc3e5e0162c957
MD5 541c8a51e2c78d57bdb380d7a8b20e7f
BLAKE2b-256 d1c9e48d47f049d6a40ac812d255b07bb0e34c8c0bcfe4aabc2a2bbfeb2f2a0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for toml_decouple-0.11.0-py3-none-any.whl:

Publisher: publish.yaml on blfpd/toml-decouple

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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