1515import logging
1616import platform
1717import shlex
18+ from functools import lru_cache
1819
1920
2021cache_lock = threading .Lock ()
@@ -38,18 +39,64 @@ def add_python_path_to_env():
3839 os .environ ['PATH' ] = os .path .dirname (sys .executable )+ sep + os .environ ['PATH' ]
3940
4041
42+ @lru_cache (maxsize = 2 )
43+ def get_pip_cmd (force_uv = False ):
44+ """
45+ Get the base pip command, with automatic fallback to uv if pip is unavailable.
46+
47+ Args:
48+ force_uv (bool): If True, use uv directly without trying pip
49+
50+ Returns:
51+ list: Base command for pip operations
52+ """
53+ embedded = 'python_embeded' in sys .executable
54+
55+ # Try pip first (unless forcing uv)
56+ if not force_uv :
57+ try :
58+ test_cmd = [sys .executable ] + (['-s' ] if embedded else []) + ['-m' , 'pip' , '--version' ]
59+ subprocess .check_output (test_cmd , stderr = subprocess .DEVNULL , timeout = 5 )
60+ return [sys .executable ] + (['-s' ] if embedded else []) + ['-m' , 'pip' ]
61+ except Exception :
62+ logging .warning ("[ComfyUI-Manager] python -m pip not available. Falling back to uv." )
63+
64+ # Try uv (either forced or pip failed)
65+ import shutil
66+
67+ # Try uv as Python module
68+ try :
69+ test_cmd = [sys .executable ] + (['-s' ] if embedded else []) + ['-m' , 'uv' , '--version' ]
70+ subprocess .check_output (test_cmd , stderr = subprocess .DEVNULL , timeout = 5 )
71+ logging .info ("[ComfyUI-Manager] Using uv as Python module for pip operations." )
72+ return [sys .executable ] + (['-s' ] if embedded else []) + ['-m' , 'uv' , 'pip' ]
73+ except Exception :
74+ pass
75+
76+ # Try standalone uv
77+ if shutil .which ('uv' ):
78+ logging .info ("[ComfyUI-Manager] Using standalone uv for pip operations." )
79+ return ['uv' , 'pip' ]
80+
81+ # Nothing worked
82+ logging .error ("[ComfyUI-Manager] Neither python -m pip nor uv are available. Cannot proceed with package operations." )
83+ raise Exception ("Neither pip nor uv are available for package management" )
84+
85+
4186def make_pip_cmd (cmd ):
42- if 'python_embeded' in sys .executable :
43- if use_uv :
44- return [sys .executable , '-s' , '-m' , 'uv' , 'pip' ] + cmd
45- else :
46- return [sys .executable , '-s' , '-m' , 'pip' ] + cmd
47- else :
48- # FIXED: https://github.com/ltdrdata/ComfyUI-Manager/issues/1667
49- if use_uv :
50- return [sys .executable , '-m' , 'uv' , 'pip' ] + cmd
51- else :
52- return [sys .executable , '-m' , 'pip' ] + cmd
87+ """
88+ Create a pip command by combining the cached base pip command with the given arguments.
89+
90+ Args:
91+ cmd (list): List of pip command arguments (e.g., ['install', 'package'])
92+
93+ Returns:
94+ list: Complete command list ready for subprocess execution
95+ """
96+ global use_uv
97+ base_cmd = get_pip_cmd (force_uv = use_uv )
98+ return base_cmd + cmd
99+
53100
54101# DON'T USE StrictVersion - cannot handle pre_release version
55102# try:
0 commit comments