[BE] always use uv pip if possible in pip_init.py for lintrunner init (#157199)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/157199
Approved by: https://github.com/ezyang, https://github.com/ZainRizvi
This commit is contained in:
Xuehai Pan 2025-07-15 13:42:34 +08:00 committed by PyTorch MergeBot
parent a78fb63dbd
commit 637e75433c
2 changed files with 37 additions and 20 deletions

View File

@ -13,17 +13,20 @@ import sys
import time import time
def run_command(args: list[str]) -> subprocess.CompletedProcess[bytes]: def run_command(
args: list[str],
env: dict[str, str] | None = None,
) -> subprocess.CompletedProcess[str]:
logging.debug("$ %s", " ".join(args)) logging.debug("$ %s", " ".join(args))
start_time = time.monotonic() start_time = time.monotonic()
try: try:
return subprocess.run(args, check=True) return subprocess.run(args, env=env, text=True, encoding="utf-8", check=True)
finally: finally:
end_time = time.monotonic() end_time = time.monotonic()
logging.debug("took %dms", (end_time - start_time) * 1000) logging.debug("took %dms", (end_time - start_time) * 1000)
if __name__ == "__main__": def main() -> None:
parser = argparse.ArgumentParser(description="pip initializer") parser = argparse.ArgumentParser(description="pip initializer")
parser.add_argument( parser.add_argument(
"packages", "packages",
@ -52,17 +55,16 @@ if __name__ == "__main__":
stream=sys.stderr, stream=sys.stderr,
) )
uv_available = ( env: dict[str, str] = {
any(prefix in sys.base_prefix for prefix in ["uv/python", "uv\\python"]) **os.environ,
and shutil.which("uv") is not None "UV_PYTHON": sys.executable,
) "UV_PYTHON_DOWNLOADS": "never",
"FORCE_COLOR": "1",
if uv_available: "CLICOLOR_FORCE": "1",
pip_args = ["uv", "pip", "install"] }
elif sys.executable: uv_index = env.get("UV_INDEX", env.get("PIP_EXTRA_INDEX_URL"))
pip_args = [sys.executable, "-mpip", "install"] if uv_index:
else: env["UV_INDEX"] = uv_index
pip_args = ["pip3", "install"]
# If we are in a global install, use `--user` to install so that you do not # If we are in a global install, use `--user` to install so that you do not
# need root access in order to initialize linters. # need root access in order to initialize linters.
@ -70,9 +72,20 @@ if __name__ == "__main__":
# However, `pip install --user` interacts poorly with virtualenvs (see: # However, `pip install --user` interacts poorly with virtualenvs (see:
# https://bit.ly/3vD4kvl) and conda (see: https://bit.ly/3KG7ZfU). So in # https://bit.ly/3vD4kvl) and conda (see: https://bit.ly/3KG7ZfU). So in
# these cases perform a regular installation. # these cases perform a regular installation.
in_conda = os.environ.get("CONDA_PREFIX") is not None in_conda = env.get("CONDA_PREFIX") is not None
in_virtualenv = os.environ.get("VIRTUAL_ENV") is not None in_virtualenv = env.get("VIRTUAL_ENV") is not None
if not in_conda and not in_virtualenv: need_user_flag = not in_conda and not in_virtualenv
uv: str | None = shutil.which("uv")
is_uv_managed_python = "uv/python" in sys.base_prefix.replace("\\", "/")
if uv and (is_uv_managed_python or not need_user_flag):
pip_args = [uv, "pip", "install"]
elif sys.executable:
pip_args = [sys.executable, "-mpip", "install"]
else:
pip_args = ["pip3", "install"]
if need_user_flag:
pip_args.append("--user") pip_args.append("--user")
pip_args.extend(args.packages) pip_args.extend(args.packages)
@ -92,4 +105,8 @@ if __name__ == "__main__":
print(f"Would have run: {pip_args}") print(f"Would have run: {pip_args}")
sys.exit(0) sys.exit(0)
run_command(pip_args) run_command(pip_args, env=env)
if __name__ == "__main__":
main()

View File

@ -250,6 +250,7 @@ class Venv:
self._env = { self._env = {
"PIP_EXTRA_INDEX_URL": self.pip_source.index_url, "PIP_EXTRA_INDEX_URL": self.pip_source.index_url,
"UV_INDEX": self.pip_source.index_url, "UV_INDEX": self.pip_source.index_url,
"UV_PYTHON_DOWNLOADS": "never",
"FORCE_COLOR": "1", "FORCE_COLOR": "1",
"CLICOLOR_FORCE": "1", "CLICOLOR_FORCE": "1",
} }
@ -475,13 +476,12 @@ class Venv:
cmd = [str(self.bindir / "uv"), *args] cmd = [str(self.bindir / "uv"), *args]
env = popen_kwargs.pop("env", None) or {} env = popen_kwargs.pop("env", None) or {}
check = popen_kwargs.pop("check", True) check = popen_kwargs.pop("check", True)
env["UV_PYTHON"] = str(python)
return subprocess.run( return subprocess.run(
cmd, cmd,
check=check, check=check,
text=True, text=True,
encoding="utf-8", encoding="utf-8",
env={**self._env, **env}, env={**self._env, **env, "UV_PYTHON": str(python)},
**popen_kwargs, **popen_kwargs,
) )