From 8b70ea01a2c5fbb4c56688d792eb991a945a5c1b Mon Sep 17 00:00:00 2001 From: StableLlama Date: Thu, 26 Mar 2026 22:39:22 +0100 Subject: [PATCH] Better handle missing development dependencies --- src/basic_data_handling/path_nodes.py | 43 ++++++++++++++++++++++--- src/basic_data_handling/tensor_nodes.py | 9 +++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/basic_data_handling/path_nodes.py b/src/basic_data_handling/path_nodes.py index 14926d8..9274bea 100644 --- a/src/basic_data_handling/path_nodes.py +++ b/src/basic_data_handling/path_nodes.py @@ -25,11 +25,44 @@ except: get_output_directory = get_input_directory +def _require_numpy(): + try: + import numpy as np + return np + except ModuleNotFoundError as e: + raise ModuleNotFoundError( + "basic_data_handling: Missing dependency 'numpy'. It seems your ComfyUI installation is faulty." + "Only for development purposes: Install it with `pip install .[dev] numpy torch pillow` or `pip install numpy`." + ) from e + + +def _require_torch(): + try: + import torch + return torch + except ModuleNotFoundError as e: + raise ModuleNotFoundError( + "basic_data_handling: Missing dependency 'torch'. It seems your ComfyUI installation is faulty." + "Only for development purposes: Install it with `pip install .[dev] numpy torch pillow` or `pip install torch`." + ) from e + + +def _require_pillow(): + try: + from PIL import Image, ImageOps + return Image, ImageOps + except ModuleNotFoundError as e: + raise ModuleNotFoundError( + "basic_data_handling: Missing dependency 'pillow'. It seems your ComfyUI installation is faulty." + "Only for development purposes: Install it with `pip install .[dev] numpy torch pillow` or `pip install pillow`." + ) from e + + # helper functions: def load_image_helper(path: str): """Helper function to load an image from a path""" - from PIL import Image, ImageOps + Image, ImageOps = _require_pillow() try: import pillow_jxl # noqa: F401 - imported but unused, kept for JPEG XL support except ModuleNotFoundError: @@ -49,8 +82,8 @@ def load_image_helper(path: str): def extract_mask_from_alpha(img): """Extract a mask from the alpha channel of an image""" - import numpy as np - import torch + np = _require_numpy() + torch = _require_torch() if 'A' in img.getbands(): alpha = np.array(img.getchannel('A')).astype(np.float32) / 255.0 @@ -70,8 +103,8 @@ def extract_mask_from_alpha(img): def extract_mask_from_greyscale(img): """Extract a mask from a greyscale image or the red channel of an RGB image""" - import numpy as np - import torch + np = _require_numpy() + torch = _require_torch() if img.mode == 'L': # Image is already greyscale diff --git a/src/basic_data_handling/tensor_nodes.py b/src/basic_data_handling/tensor_nodes.py index 9b825d6..52b4683 100644 --- a/src/basic_data_handling/tensor_nodes.py +++ b/src/basic_data_handling/tensor_nodes.py @@ -1,4 +1,11 @@ -import torch +try: + import torch +except ModuleNotFoundError as e: + raise ModuleNotFoundError( + "basic_data_handling: Missing dependency 'torch'. It seems your ComfyUI installation is faulty." + "Only for development purposes: Install it with `pip install .[dev] numpy torch pillow` or `pip install torch`." + ) from e + from inspect import cleandoc from typing import Any