From 16b25c748df1dd62895d52376f6a1aecf025fafe Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:16:59 -0400 Subject: [PATCH 1/7] patch sys.path inside the comfyui process --- lib_comfyui/comfyui/pre_main.py | 40 ++++++++++++--------------------- lib_comfyui/comfyui_process.py | 26 ++++++--------------- 2 files changed, 21 insertions(+), 45 deletions(-) diff --git a/lib_comfyui/comfyui/pre_main.py b/lib_comfyui/comfyui/pre_main.py index e1ef09d..d4b7526 100644 --- a/lib_comfyui/comfyui/pre_main.py +++ b/lib_comfyui/comfyui/pre_main.py @@ -1,8 +1,20 @@ +import os +import sys + +if __name__ == "__main__": + # patch sys.path + install_dir = os.getenv("SD_WEBUI_COMFYUI_INSTALL_DIR") + extension_dir = os.getenv("SD_WEBUI_COMFYUI_EXTENSION_DIR") + if not install_dir or not extension_dir: + print("[sd-webui-comfyui]", f"Could not add new entries to sys.path. install_dir={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] = (install_dir, extension_dir) + import atexit import builtins import signal -import sys -import os import runpy from lib_comfyui import ( custom_extension_injector, @@ -20,35 +32,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...') diff --git a/lib_comfyui/comfyui_process.py b/lib_comfyui/comfyui_process.py index b6bee89..ff092c4 100644 --- a/lib_comfyui/comfyui_process.py +++ b/lib_comfyui/comfyui_process.py @@ -35,7 +35,7 @@ def start_comfyui_process(comfyui_install_location): global comfyui_process executable = get_comfyui_executable(comfyui_install_location) - comfyui_env = get_comfyui_env(executable, comfyui_install_location) + comfyui_env = get_comfyui_env(comfyui_install_location) install_comfyui_requirements(executable, comfyui_install_location, comfyui_env) args = [executable, inspect.getfile(pre_main)] + argv_conversion.get_comfyui_args() comfyui_process = subprocess.Popen( @@ -60,29 +60,17 @@ def get_comfyui_executable(comfyui_install_location): return str(executable) -def get_comfyui_env(executable, comfyui_install_location): +def get_comfyui_env(comfyui_install_location): comfyui_env = os.environ.copy() - comfyui_sys_path = get_base_sys_path(executable, comfyui_install_location) - comfyui_sys_path[:0] = (str(comfyui_install_location), settings.get_extension_base_dir()) - comfyui_env['PYTHONPATH'] = os.pathsep.join(comfyui_sys_path) + if 'PYTHONPATH' in comfyui_env: + del comfyui_env['PYTHONPATH'] + + comfyui_env['SD_WEBUI_COMFYUI_INSTALL_DIR'] = str(comfyui_install_location) + comfyui_env['SD_WEBUI_COMFYUI_EXTENSION_DIR'] = settings.get_extension_base_dir() comfyui_env['SD_WEBUI_COMFYUI_IPC_STRATEGY_CLASS_NAME'] = global_state.ipc_strategy_class.__name__ return comfyui_env -def get_base_sys_path(executable, comfyui_install_location): - env = os.environ.copy() - if 'PYTHONPATH' in env: - del env['PYTHONPATH'] - return subprocess.run( - args=[executable, inspect.getfile(print_sys_path)], - executable=executable, - cwd=str(comfyui_install_location), - env=env, - text=True, - capture_output=True, - ).stdout.split(os.pathsep)[1:] # remove PYTHONHOME because it is automatically added - - def install_comfyui_requirements(executable, comfyui_install_location, comfyui_env): if executable == sys.executable: # requirements already installed in the webui by install.py From e81861b2ceea7d21f47cb413f3d4f9e8a093b049 Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:20:13 -0400 Subject: [PATCH 2/7] rm unused code --- lib_comfyui/comfyui/print_sys_path.py | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 lib_comfyui/comfyui/print_sys_path.py diff --git a/lib_comfyui/comfyui/print_sys_path.py b/lib_comfyui/comfyui/print_sys_path.py deleted file mode 100644 index 10133fa..0000000 --- a/lib_comfyui/comfyui/print_sys_path.py +++ /dev/null @@ -1,6 +0,0 @@ -import sys -import os - - -if __name__ == '__main__': - print(os.pathsep.join(sys.path), end='') From 7de618a0dd297c256686e3f31d5917ea8cdc34c0 Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:20:50 -0400 Subject: [PATCH 3/7] rm unused code --- lib_comfyui/comfyui_process.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_comfyui/comfyui_process.py b/lib_comfyui/comfyui_process.py index ff092c4..4984fd3 100644 --- a/lib_comfyui/comfyui_process.py +++ b/lib_comfyui/comfyui_process.py @@ -7,7 +7,7 @@ from lib_comfyui import ipc, torch_utils, argv_conversion, global_state from lib_comfyui.webui import settings -from lib_comfyui.comfyui import pre_main, print_sys_path +from lib_comfyui.comfyui import pre_main comfyui_process = None From bc30b099a7df5290e98006d789ac18e5b98e2fb2 Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:31:15 -0400 Subject: [PATCH 4/7] use cwd --- lib_comfyui/comfyui/pre_main.py | 2 +- lib_comfyui/comfyui_process.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib_comfyui/comfyui/pre_main.py b/lib_comfyui/comfyui/pre_main.py index d4b7526..30f8b62 100644 --- a/lib_comfyui/comfyui/pre_main.py +++ b/lib_comfyui/comfyui/pre_main.py @@ -3,7 +3,7 @@ if __name__ == "__main__": # patch sys.path - install_dir = os.getenv("SD_WEBUI_COMFYUI_INSTALL_DIR") + install_dir = os.getcwd() # comfyui dir extension_dir = os.getenv("SD_WEBUI_COMFYUI_EXTENSION_DIR") if not install_dir or not extension_dir: print("[sd-webui-comfyui]", f"Could not add new entries to sys.path. install_dir={install_dir}, extension_dir={extension_dir}, sys.path={sys.path}", file=sys.stderr) diff --git a/lib_comfyui/comfyui_process.py b/lib_comfyui/comfyui_process.py index 4984fd3..36c5228 100644 --- a/lib_comfyui/comfyui_process.py +++ b/lib_comfyui/comfyui_process.py @@ -65,7 +65,6 @@ def get_comfyui_env(comfyui_install_location): if 'PYTHONPATH' in comfyui_env: del comfyui_env['PYTHONPATH'] - comfyui_env['SD_WEBUI_COMFYUI_INSTALL_DIR'] = str(comfyui_install_location) comfyui_env['SD_WEBUI_COMFYUI_EXTENSION_DIR'] = settings.get_extension_base_dir() comfyui_env['SD_WEBUI_COMFYUI_IPC_STRATEGY_CLASS_NAME'] = global_state.ipc_strategy_class.__name__ return comfyui_env From c33a490de0f59bc258ee16db62d346647b6065fc Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:32:23 -0400 Subject: [PATCH 5/7] rename --- lib_comfyui/comfyui/pre_main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib_comfyui/comfyui/pre_main.py b/lib_comfyui/comfyui/pre_main.py index 30f8b62..071afab 100644 --- a/lib_comfyui/comfyui/pre_main.py +++ b/lib_comfyui/comfyui/pre_main.py @@ -3,14 +3,14 @@ if __name__ == "__main__": # patch sys.path - install_dir = os.getcwd() # comfyui dir + comfyui_install_dir = os.getcwd() extension_dir = os.getenv("SD_WEBUI_COMFYUI_EXTENSION_DIR") - if not install_dir or not 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={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] = (install_dir, extension_dir) + sys.path[:0] = (comfyui_install_dir, extension_dir) import atexit import builtins From 37e5a3b7d636c93de064299fe94d6848842949ce Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:32:34 -0400 Subject: [PATCH 6/7] rename --- lib_comfyui/comfyui/pre_main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_comfyui/comfyui/pre_main.py b/lib_comfyui/comfyui/pre_main.py index 071afab..7a6e746 100644 --- a/lib_comfyui/comfyui/pre_main.py +++ b/lib_comfyui/comfyui/pre_main.py @@ -6,7 +6,7 @@ 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={install_dir}, extension_dir={extension_dir}, sys.path={sys.path}", file=sys.stderr) + 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) From 19583fd36ef5e03238a03f7edccdbd6c850f6aec Mon Sep 17 00:00:00 2001 From: ljleb Date: Thu, 31 Aug 2023 02:36:48 -0400 Subject: [PATCH 7/7] def --- lib_comfyui/comfyui/pre_main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib_comfyui/comfyui/pre_main.py b/lib_comfyui/comfyui/pre_main.py index 7a6e746..523a391 100644 --- a/lib_comfyui/comfyui/pre_main.py +++ b/lib_comfyui/comfyui/pre_main.py @@ -1,8 +1,8 @@ import os import sys -if __name__ == "__main__": - # patch sys.path + +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: @@ -12,6 +12,11 @@ sys.path[:0] = (comfyui_install_dir, extension_dir) + +if __name__ == "__main__": + patch_sys_path() + + import atexit import builtins import signal