Skip to content

Instantly share code, notes, and snippets.

@MrPandir
Last active April 24, 2025 10:26
Show Gist options
  • Save MrPandir/61d48066c4ad7beadfc36672dcb750d6 to your computer and use it in GitHub Desktop.
Save MrPandir/61d48066c4ad7beadfc36672dcb750d6 to your computer and use it in GitHub Desktop.
Lightweight Python .env file reader and environment variable loader
from os import environ
from pathlib import Path
__all__ = ["load_env_to_environ"]
def _format(key_or_value: str) -> str:
return key_or_value.strip(" \"'")
def _get_env_data_as_dict(path: Path | str) -> dict[str, str]:
with open(path, "r") as f:
return dict(
list(map(_format, line.replace("\n", "").split("=", 1)))
for line in f.readlines()
if not line.startswith("#") and line.count("=") >= 1
)
def load_env_to_environ(path: Path | str = ".", quiet=True):
path = Path(path)
if path.is_dir():
path = path / ".env"
try:
vars_dict = _get_env_data_as_dict(path)
environ.update(vars_dict)
except Exception:
if not quiet:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment