diff --git a/__init__.py b/__init__.py index 5ac41c0..d5d1cdf 100644 --- a/__init__.py +++ b/__init__.py @@ -11,8 +11,40 @@ as `__init__.py.legacy`, alongside the other legacy modules (`routes.py`, `workflows.py`, `imagelab_nodes.py`, `resources.py`, ...) — kept for reference, not loaded. """ +import os import threading + +def _load_env_file() -> None: + """Populate os.environ from the extension's `.env` file. + + ComfyUI is launched directly via `python main.py`, so nothing injects + these vars. Without this, CIVITAI_TOKEN / IMAGELAB_AUTO_DOWNLOAD sit + unread in .env and downloads fail with a 401. + + Existing env vars win — the shell/pod template can still override. + Must run before importing modules that read env at import time. + """ + env_path = os.path.join(os.path.dirname(__file__), ".env") + if not os.path.isfile(env_path): + return + try: + with open(env_path, "r", encoding="utf-8") as fh: + for raw in fh: + 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 key and key not in os.environ: + os.environ[key] = value + except Exception as e: + print(f"[ImageLab] Failed to read .env: {e}") + + +_load_env_file() + from server import PromptServer from . import hashing