diff --git a/lib_comfyui/comfyui/pre_main.py b/lib_comfyui/comfyui/pre_main.py index e1ef09d..523a391 100644 --- a/lib_comfyui/comfyui/pre_main.py +++ b/lib_comfyui/comfyui/pre_main.py @@ -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, @@ -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...') 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='') diff --git a/lib_comfyui/comfyui_process.py b/lib_comfyui/comfyui_process.py index b6bee89..36c5228 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 @@ -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,16 @@ 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_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