Handle parent packages to allow for relative imports

This commit is contained in:
WAS 2023-06-27 15:03:08 -07:00 committed by GitHub
parent d8a953ef53
commit 15385b592d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1421,34 +1421,37 @@ def load_custom_node(module_path):
else:
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):
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:
module_name = os.path.basename(module_path)
init_file_path = os.path.join(module_path, "__init__.py")
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:
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:
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()
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:
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:
NODE_DISPLAY_NAME_MAPPINGS.update(sub_module.NODE_DISPLAY_NAME_MAPPINGS)
# Update modified times for the main module path
update_modified_times(module_path)
return True
try:
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:
NODE_CLASS_MAPPINGS.update(module.NODE_CLASS_MAPPINGS)
@ -1463,7 +1466,7 @@ def load_custom_node(module_path):
print(traceback.format_exc())
print(f"Cannot import {module_path} module for custom nodes:", e)
update_modified_times(module_path)
return False
return False
def load_custom_nodes():
node_paths = folder_paths.get_folder_paths("custom_nodes")