-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimport_utils.py
More file actions
38 lines (28 loc) · 1.17 KB
/
import_utils.py
File metadata and controls
38 lines (28 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""import_utils.py
Small helpers to make local (same-folder) imports robust inside ComfyUI custom nodes.
This module is intentionally tiny and dependency-free.
"""
from __future__ import annotations
import os
import sys
from typing import Optional
def ensure_local_import(file_path: str, *, prepend: bool = True) -> str:
"""Ensure the directory containing *file_path* is present in sys.path.
ComfyUI sometimes loads custom nodes in a way that does not guarantee the node
folder is on sys.path. Several nodes in this repo import sibling modules
(e.g. vision_llm_node.py). This helper makes that import reliable.
Args:
file_path: Usually pass __file__ from the caller module.
prepend: If True, insert at sys.path[0]; else append.
Returns:
The normalized directory path that was inserted/ensured.
"""
node_dir = os.path.dirname(os.path.abspath(file_path))
# Normalize for stable comparisons across platforms
node_dir = os.path.normpath(node_dir)
if node_dir and node_dir not in sys.path:
if prepend:
sys.path.insert(0, node_dir)
else:
sys.path.append(node_dir)
return node_dir