import os import pathlib import logging logger = logging.getLogger("qkernel.env") _ENV_CANDIDATES = [ pathlib.Path(__file__).resolve().parent.parent.parent / ".env", pathlib.Path(__file__).resolve().parent.parent.parent.parent / ".env", ] def load_env() -> None: for env_file in _ENV_CANDIDATES: if not env_file.exists(): continue try: for raw in env_file.read_text(encoding="utf-8").splitlines(): line = raw.strip() if not line or line.startswith("#") or "=" not in line: continue key, _, value = line.partition("=") key = key.strip() value = value.strip().strip('"').strip("'") if not key: continue os.environ.setdefault(key, value) logger.info("Loaded env from %s", env_file) return except OSError as e: logger.warning("Could not read env %s: %s", env_file, e) logger.info("No .env file found; using process environment") load_env()