diff --git a/src/huggingface_hub/cli/system.py b/src/huggingface_hub/cli/system.py index 8ffd2fed33b..970f2346709 100644 --- a/src/huggingface_hub/cli/system.py +++ b/src/huggingface_hub/cli/system.py @@ -46,6 +46,18 @@ def update() -> None: out.text(f"hf is up to date ({__version__})") return + # Windows refuses to replace the `hf.exe` launcher of a running process: pip would fail with a + # "file in use" error (WinError 32) *after* having uninstalled the current version, leaving a dead + # `hf.exe` and no `huggingface_hub` module behind. Print the command instead of running it, the same + # way pip refuses to upgrade itself on Windows. + if sys.platform == "win32" and installation_method() == "pip": + command = subprocess.list2cmdline([sys.executable, "-m", "pip", "install", "-U", "huggingface_hub"]) + out.error( + "On Windows, a pip-installed `hf` cannot update itself: pip is not allowed to replace `hf.exe` while it " + f"is running. Run this command instead:\n {command}" + ) + raise click.exceptions.Exit(code=1) + # The standalone installer installs the `hf-cli` skill by default. If it's not installed at this # point, the user opted out (or removed it): tell the installer to leave it alone instead of # silently undoing that choice. diff --git a/tests/test_cli.py b/tests/test_cli.py index a32af2a10e0..bf01baf8811 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -5009,6 +5009,8 @@ def _mocked_update(self) -> Generator[Mock, None, None]: with ( patch("huggingface_hub.cli.system._fetch_latest_pypi_version", return_value="99.0.0"), patch("huggingface_hub.cli.system.subprocess.call", return_value=0), + # `hf update` refuses to self-update a pip install on Windows: pretend we're not on Windows. + patch("huggingface_hub.cli.system.sys.platform", "linux"), patch("huggingface_hub.cli.system.run_update", return_value=0) as mock_run_update, ): yield mock_run_update