Skip to content

__init__.py: importlib.import_module() outside try/except crashes entire plugin #602

@WSHAPER

Description

@WSHAPER

Bug

In __init__.py line 35, importlib.import_module() is called outside the try/except block:

for file in files:
    if not file.endswith(".py"):
        continue
    name = os.path.splitext(file)[0]
    imported_module = importlib.import_module(".py.{}".format(name), __name__)  # <-- outside try
    try:
        NODE_CLASS_MAPPINGS = {**NODE_CLASS_MAPPINGS, **imported_module.NODE_CLASS_MAPPINGS}
        ...
    except:
        pass

Impact

If any single .py module fails to import, the entire plugin fails to load and none of its nodes register. The except: pass was clearly intended to skip broken modules gracefully, but the import_module call isn't covered by it.

Reproduction

In the current main branch, color_name.py references find_best_match_by_similarity from imagefunc.py, but that function is not defined anywhere:

ImportError: cannot import name 'find_best_match_by_similarity' from 'comfyui_layerstyle.py.imagefunc'

This crashes the entire plugin — all 164+ nodes become unavailable, including unrelated ones like GaussianBlurV2, QWenImage2Prompt, etc.

Fix

Wrap the import_module call inside the existing try/except:

for file in files:
    if not file.endswith(".py"):
        continue
    name = os.path.splitext(file)[0]
    try:
        imported_module = importlib.import_module(".py.{}".format(name), __name__)
        NODE_CLASS_MAPPINGS = {**NODE_CLASS_MAPPINGS, **imported_module.NODE_CLASS_MAPPINGS}
        ...
    except:
        pass

This ensures that a broken module is silently skipped as originally intended, without taking down the rest of the plugin.

Environment

  • ComfyUI latest
  • Python 3.11
  • ComfyUI_LayerStyle at d94bef1 (current main HEAD)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions