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.tomlfile;override:- if
os.environdoes not have the environment variables, it will load the environment variables from the.env.tomlfile; - if
os.environalready has the environment variables, andoverride=True, it will override the environment variables with the values in the.env.tomlfile; - if
os.environalready has the environment variables, andoverride=False, it will not override the environment variables with the values in the.env.tomlfile;
- if
get method
get(key: str, default=None, nullable=False) -> Anykey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_str method
get_str(key: str, default=None, nullable=False) -> strkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_int method
get_int(key: str, default=None, nullable=False) -> intkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_float method
get_float(key: str, default=None, nullable=False) -> floatkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_bool method
get_bool(key: str, default=None, nullable=False) -> boolkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
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)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pytomlenv-0.2.0.tar.gz | 3.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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.