Load .env on extension startup so CIVITAI_TOKEN actually reaches downloads

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) <noreply@anthropic.com>
This commit is contained in:
donnyandClaude Opus 4.7 committed 2026-05-15 11:57:12 -07:00
1 parent 907ae5fe2b
commit 34dffbc832
1 file changed
+32
+32
View File
@@ -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