mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-19 23:56:59 +08:00
Handle parent packages to allow for relative imports
This commit is contained in:
parent
d8a953ef53
commit
15385b592d
17
nodes.py
17
nodes.py
@ -1421,34 +1421,37 @@ def load_custom_node(module_path):
|
|||||||
else:
|
else:
|
||||||
NODE_MODIFICATION_TIMES[module_path] = os.path.getmtime(module_path)
|
NODE_MODIFICATION_TIMES[module_path] = os.path.getmtime(module_path)
|
||||||
|
|
||||||
|
|
||||||
|
parent_dir = os.path.dirname(module_path)
|
||||||
|
parent_package = os.path.basename(parent_dir)
|
||||||
|
|
||||||
if os.path.isfile(module_path):
|
if os.path.isfile(module_path):
|
||||||
module_name = os.path.splitext(os.path.basename(module_path))[0]
|
module_name = os.path.splitext(os.path.basename(module_path))[0]
|
||||||
loader = importlib.machinery.SourceFileLoader(module_name, module_path)
|
loader = importlib.machinery.SourceFileLoader(f"{parent_package}.{module_name}", module_path)
|
||||||
else:
|
else:
|
||||||
module_name = os.path.basename(module_path)
|
module_name = os.path.basename(module_path)
|
||||||
init_file_path = os.path.join(module_path, "__init__.py")
|
init_file_path = os.path.join(module_path, "__init__.py")
|
||||||
if os.path.exists(init_file_path):
|
if os.path.exists(init_file_path):
|
||||||
loader = importlib.machinery.SourceFileLoader(module_name, init_file_path)
|
loader = importlib.machinery.SourceFileLoader(f"{parent_package}.{module_name}", init_file_path)
|
||||||
else:
|
else:
|
||||||
file_paths = [os.path.join(module_path, file_name) for file_name in os.listdir(module_path) if file_name.endswith(".py")]
|
file_paths = [os.path.join(module_path, file_name) for file_name in os.listdir(module_path) if file_name.endswith(".py")]
|
||||||
for file_path in file_paths:
|
for file_path in file_paths:
|
||||||
sub_module_name = os.path.splitext(os.path.basename(file_path))[0]
|
sub_module_name = os.path.splitext(os.path.basename(file_path))[0]
|
||||||
sub_loader = importlib.machinery.SourceFileLoader(sub_module_name, file_path)
|
sub_loader = importlib.machinery.SourceFileLoader(f"{parent_package}.{module_name}.{sub_module_name}", file_path)
|
||||||
sub_module = sub_loader.load_module()
|
sub_module = sub_loader.load_module()
|
||||||
sys.modules[sub_module_name] = sub_module
|
sys.modules[f"{parent_package}.{module_name}.{sub_module_name}"] = sub_module
|
||||||
|
|
||||||
if hasattr(sub_module, "NODE_CLASS_MAPPINGS") and getattr(sub_module, "NODE_CLASS_MAPPINGS") is not None:
|
if hasattr(sub_module, "NODE_CLASS_MAPPINGS") and getattr(sub_module, "NODE_CLASS_MAPPINGS") is not None:
|
||||||
NODE_CLASS_MAPPINGS.update(sub_module.NODE_CLASS_MAPPINGS)
|
NODE_CLASS_MAPPINGS.update(sub_module.NODE_CLASS_MAPPINGS)
|
||||||
if hasattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") is not None:
|
if hasattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") and getattr(sub_module, "NODE_DISPLAY_NAME_MAPPINGS") is not None:
|
||||||
NODE_DISPLAY_NAME_MAPPINGS.update(sub_module.NODE_DISPLAY_NAME_MAPPINGS)
|
NODE_DISPLAY_NAME_MAPPINGS.update(sub_module.NODE_DISPLAY_NAME_MAPPINGS)
|
||||||
|
|
||||||
# Update modified times for the main module path
|
|
||||||
update_modified_times(module_path)
|
update_modified_times(module_path)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
module = loader.load_module()
|
module = loader.load_module()
|
||||||
sys.modules[module_name] = module
|
sys.modules[f"{parent_package}.{module_name}"] = module
|
||||||
|
|
||||||
if hasattr(module, "NODE_CLASS_MAPPINGS") and getattr(module, "NODE_CLASS_MAPPINGS") is not None:
|
if hasattr(module, "NODE_CLASS_MAPPINGS") and getattr(module, "NODE_CLASS_MAPPINGS") is not None:
|
||||||
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
|
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
|
||||||
@ -1463,7 +1466,7 @@ def load_custom_node(module_path):
|
|||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
print(f"Cannot import {module_path} module for custom nodes:", e)
|
print(f"Cannot import {module_path} module for custom nodes:", e)
|
||||||
update_modified_times(module_path)
|
update_modified_times(module_path)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def load_custom_nodes():
|
def load_custom_nodes():
|
||||||
node_paths = folder_paths.get_folder_paths("custom_nodes")
|
node_paths = folder_paths.get_folder_paths("custom_nodes")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user