Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch sys.path inside the comfyui process #155

Merged
merged 7 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 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
6 changes: 0 additions & 6 deletions lib_comfyui/comfyui/print_sys_path.py

This file was deleted.

27 changes: 7 additions & 20 deletions lib_comfyui/comfyui_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down