Skip to main content

pytomlenv

Environment in TOML for Python.

Install

pip install pytomlenv

Usage

.env.toml

[production]
DEBUG = false
DB_ENGINE = "django.db.backends.postgresql"
DB_NAME = "ragx"
DB_USER = "postgres"
DB_PASSWORD = "123456"
DB_HOST = "10.10.0.100"
DB_PORT = 5432

[development]
DEBUG = true
DB_ENGINE = "django.db.backends.sqlite3"
DB_NAME = "ragx.db"
DB_USER = ""
DB_PASSWORD = ""
DB_HOST = ""
DB_PORT = 0

[test]
DEBUG = true
DB_ENGINE = "django.db.backends.mysql"
DB_NAME = "ragx"
DB_USER = "root"
DB_PASSWORD = "123456"
DB_HOST = "127.0.0.1"
DB_PORT = 3306

your python code:

from toml_env import load_env

env = load_env(".env.toml")

# `os.environ.get()` also works, but it always return string

# you have better choice with the following methods:

project: str = env.get("PROJECT")
db_name: str = env.get_str("DB_NAME")
db_user: str = env.get_str("DB_USER", "root")
db_pass: str = env.get_str("DB_PASS", "password")
db_host: str = env.get_str("DB_HOST", "localhost")
db_port: int = env.get_int("DB_PORT", 3306)

debug: bool = env.get_bool("DEBUG", False)
sentry_dsn: str = env.get_str("SENTRY_DSN", None, nullable=True)

print(debug, db_name, db_user, db_pass, db_host, db_port, sentry_dsn)

run your python code:

$ export ENV=development
$ python your_code.py

Methods

load_env function

def load_env(path: str = ".env.toml", override: bool = False)

  • path: the path to the .env.toml file;
  • override:
    • if os.environ does not have the environment variables, it will load the environment variables from the .env.toml file;
    • if os.environ already has the environment variables, and override=True, it will override the environment variables with the values in the .env.toml file;
    • if os.environ already has the environment variables, and override=False, it will not override the environment variables with the values in the .env.toml file;

get method

  • get(key: str, default=None, nullable=False) -> Any
  • key: the key in the .env.toml file;
  • default: the default value if the key is not found in the .env.toml file;
  • nullable: if the key is not found in the .env.toml file, and the default value is None, you should set nullable=True, otherwise it will raise TomlEnvError exception;

get_str method

  • get_str(key: str, default=None, nullable=False) -> str
  • key: the key in the .env.toml file;
  • default: the default value if the key is not found in the .env.toml file;
  • nullable: if the key is not found in the .env.toml file, and the default value is None, you should set nullable=True, otherwise it will raise TomlEnvError exception;

get_int method

  • get_int(key: str, default=None, nullable=False) -> int
  • key: the key in the .env.toml file;
  • default: the default value if the key is not found in the .env.toml file;
  • nullable: if the key is not found in the .env.toml file, and the default value is None, you should set nullable=True, otherwise it will raise TomlEnvError exception;

get_float method

  • get_float(key: str, default=None, nullable=False) -> float
  • key: the key in the .env.toml file;
  • default: the default value if the key is not found in the .env.toml file;
  • nullable: if the key is not found in the .env.toml file, and the default value is None, you should set nullable=True, otherwise it will raise TomlEnvError exception;

get_bool method

  • get_bool(key: str, default=None, nullable=False) -> bool
  • key: the key in the .env.toml file;
  • default: the default value if the key is not found in the .env.toml file;
  • nullable: if the key is not found in the .env.toml file, and the default value is None, you should set nullable=True, otherwise it will raise TomlEnvError exception;

Metadata

Release files for pytomlenv 0.2.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 pytomlenv 0.2.0
File Size Uploaded Provenance
pytomlenv-0.2.0.tar.gz 3.1 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 6.9 kB

Release files / pytomlenv-0.2.0.tar.gz

Download URL pytomlenv-0.2.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9c3968b432b33514048ab033ba0be4583c6e17c09f93ed6ba95a9be9fd8524ff
BLAKE2b-256 checksum
How to use checksums
367e8ba285af0f56ce88625702cba67c82d1c8b73ec1bc6332b4608dce423fef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.1 Linux/6.5.0-1025-azure

Release files / pytomlenv-0.2.0-py3-none-any.whl

Download URL pytomlenv-0.2.0-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8932c5bd8d77897ba0bea2f43f83db58cc7dc3fb48398da8d3d980c9fb12947d
BLAKE2b-256 checksum
How to use checksums
3e322df6931cfb4991a1a2ac3b100465ff285bc6db472cf8658cbb4d5caab882
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.1 Linux/6.5.0-1025-azure

Security for pytomlenv 0.2.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

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