I have a flask webapp that loads config upon startup from a hardcoded path like so:
# configuration.py
import sys
from pathlib import Path
from yaml import safe_load
prod = Path(__file__).resolve().parent.parent / "conf.yml"
dev = Path(__file__).resolve().parent.parent / "dev_conf.yml"
try:
if "pytest" in sys.modules:
config = safe_load(prod)
else:
config = safe_load(dev)
except ValueError:
raise SystemExit("Failed to load config")
This config
var is then imported and used across all the other files. E.g:
# some_other_module.py
from .configuration import config
print(f"hello {config["email"]}")
I wanna improve this because I have a feeling this isn’t the best way to achieve this. How would I go about it? My first guess is that instead of loading a hardcoded path, load it from an env var.