From 34dffbc832d22951a6ab1fe4d0c1469481523e3c Mon Sep 17 00:00:00 2001 From: donnyjang Date: Fri, 15 May 2026 11:57:12 -0700 Subject: [PATCH] Load .env on extension startup so CIVITAI_TOKEN actually reaches downloads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The .env.example told users to "copy this to .env", but nothing called load_dotenv — so os.environ.get("CIVITAI_TOKEN") was None and downloads failed with "Download requires a valid CIVITAI_TOKEN" (HTTP 401). Co-Authored-By: Claude Opus 4.7 (1M context) --- __init__.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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