Skip to main content
PyPI Status Python Versions Wheel Status PyPI Downloads Code coverage Status License Status

envTOML is an answer to a fairly simple problem: including values from environment variables in TOML configuration files. In this way, it is very similar to both envyaml and varyaml which provide very similar functionality for YAML and which greatly inspired this small package.

Under the hood it uses the standard library tomllib (and tomli as a fallback for Python < 3.11).

Supports Python 3.10+.

Example

Suppose we have the following configuration saved in config.toml

[db]
host = "$DB_HOST"
port = "$DB_PORT"
username = "$DB_USERNAME"
password = "$DB_PASSWORD"
name = "my_database"

with the environment variables being set to the following

DB_HOST=some-host.tld
DB_PORT=3306
DB_USERNAME=user01
DB_PASSWORD=veryToughPas$w0rd

this config can then be parsed with envTOML in the following way:

import envtoml

cfg = envtoml.load(open('./config.toml', 'rb'))

print(cfg)
# {'db': {'host': 'some-host.tld',
#   'port': 3306,
#   'username': 'user01',
#   'password': 'veryToughPas$w0rd',
#   'name': 'my_database'}}

You can reference multiple environment variables inside a single string:

cfg = envtoml.loads(
    "db_url = 'mysql://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME'\\n"
)

print(cfg)
# {'db_url': 'mysql://user01:veryToughPas$w0rd@some-host.tld:3306/my_database'}

Default values can be specified with ${VAR:-default}:

cfg = envtoml.loads("region = '${AWS_REGION:-us-east-1}'\\n")
# {'region': 'us-east-1'}

Literal dollar signs can be escaped with $$:

cfg = envtoml.loads("price = '$$19.99'\\n")
# {'price': '$19.99'}

Lists are supported too:

cfg = envtoml.loads("scopes = ['$SCOPE_A', '$SCOPE_B']\\n")

To fail when a referenced env var is missing, pass fail_on_missing=True. This raises ValueError when a variable is not present or is empty:

# Example: fail fast if API_TOKEN is not set.
cfg = envtoml.loads("api_token = '$API_TOKEN'\\n", fail_on_missing=True)
# Raises ValueError: API_TOKEN not found in environment

To substitute from a mapping other than os.environ, pass env_values. The mapping is used exclusively — variables set only in the process environment are not consulted. This pairs well with python-dotenv’s dotenv_values(), which reads a .env file without mutating the environment:

cfg = envtoml.loads("port = '$PORT'\\n", env_values={'PORT': '8080'})
# {'port': 8080}

from dotenv import dotenv_values
cfg = envtoml.loads("port = '$PORT'\\n", env_values=dotenv_values('.env'))

Tests

This project uses uv. After installing it, run the following from the project’s root directory:

uv sync --group dev
uv run pytest

For coverage:

uv run pytest --cov=envtoml

License

Licensed under the MIT license (see LICENSE file for more details).

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envtoml-0.5.0.tar.gz (29.2 kB view details)

Uploaded Source

Built Distribution

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

envtoml-0.5.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file envtoml-0.5.0.tar.gz.

File metadata

  • Download URL: envtoml-0.5.0.tar.gz
  • Upload date:
  • Size: 29.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for envtoml-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3750576663482a03ec9ca9e1f7a22100de626445903cc35d447cec09d1804834
MD5 961ab68622d4db9972445abc89d951da
BLAKE2b-256 3ab26df7a1547a76c4cea05feb4a18a2aed9fc1dd09d153147c5a33ec9eb079b

See more details on using hashes here.

File details

Details for the file envtoml-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: envtoml-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for envtoml-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fcb12e2a39bef104b2d607e20487bbed41b207d4c715623ee767023f97a26a4
MD5 2992b0d73f25930737ee04aa6faeed58
BLAKE2b-256 b72450e4abd1044abbf8010d7219522cfc7b3ce29feb114dd449880df087ae73

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

3 files

0.1.0

2 files

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