Skip to content

Commit

Permalink
patch sys.path inside the comfyui process (ModelSurge#155) from b20d25e
Browse files Browse the repository at this point in the history
  • Loading branch information
RGX650 authored Jun 11, 2024
1 parent 127f27f commit 7727550
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions lib_comfyui/comfyui/pre_main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import os
import sys


def patch_sys_path():
comfyui_install_dir = os.getcwd()
extension_dir = os.getenv("SD_WEBUI_COMFYUI_EXTENSION_DIR")
if not comfyui_install_dir or not extension_dir:
print("[sd-webui-comfyui]", f"Could not add new entries to sys.path. install_dir={comfyui_install_dir}, extension_dir={extension_dir}, sys.path={sys.path}", file=sys.stderr)
print("[sd-webui-comfyui]", f"Exiting...", file=sys.stderr)
exit(1)

sys.path[:0] = (comfyui_install_dir, extension_dir)


if __name__ == "__main__":
patch_sys_path()


import atexit
import builtins
import signal
import sys
import os
import runpy
from lib_comfyui import (
custom_extension_injector,
Expand All @@ -20,35 +37,11 @@ def comfyui_print(*args, **kwargs):
@ipc.restrict_to_process('comfyui')
def main():
builtins.print = comfyui_print
fix_path()
setup_ipc()
patch_comfyui()
start_comfyui()


@ipc.restrict_to_process('comfyui')
def fix_path():
def make_path_unique():
path = sys.path.copy()
sys.path.clear()
seen = set()
sys.path.extend(
p for p in path
if not (p in seen or seen.add(p))
)

def move_comfyui_to_front():
comfyui_dir = os.getcwd()
try:
sys.path.remove(comfyui_dir)
except ValueError:
pass
sys.path.insert(0, comfyui_dir)

make_path_unique()
move_comfyui_to_front()


@ipc.restrict_to_process('comfyui')
def setup_ipc():
print('[sd-webui-comfyui]', 'Setting up IPC...')
Expand Down Expand Up @@ -76,6 +69,14 @@ def exit_signal_handler(sig, frame):
@ipc.restrict_to_process('comfyui')
def patch_comfyui():
print('[sd-webui-comfyui]', 'Patching ComfyUI...')
try:
# workaround for newer versions of comfyui https://github.com/comfyanonymous/ComfyUI/commit/3039b08eb16777431946ed9ae4a63c5466336bff
# remove the try-except to stop supporting older versions
import comfy.options
comfy.options.enable_args_parsing()
except ImportError:
pass

paths.share_webui_folder_paths()
custom_extension_injector.register_webui_extensions()
routes_extension.patch_server_routes()
Expand Down

0 comments on commit 7727550

Please sign in to comment.