From ef001403a75128b444125091e51bac98612db2bc Mon Sep 17 00:00:00 2001 From: Emptyngton <40150265+emptyngton@users.noreply.github.com> Date: Sun, 26 Jul 2026 20:00:30 -0400 Subject: [PATCH] fix(loader): skip CUDA_PATH / HIP_PATH dirs that do not exist os.add_dll_directory() raises FileNotFoundError [WinError 3] when the directory does not exist, so a CUDA_PATH or HIP_PATH left pointing at an uninstalled or upgraded toolkit makes "import llama_cpp" fail outright on Windows, before any user code runs. Check each candidate directory with os.path.exists() before registering it. Directories are tested individually, so a partially removed toolkit still contributes whichever of bin/lib remain rather than raising. Reported previously in #1077, where a stale CUDA_PATH pointing at an uninstalled CUDA 11.2 broke the import on a machine with 12.3 installed. --- llama_cpp/_ctypes_extensions.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/llama_cpp/_ctypes_extensions.py b/llama_cpp/_ctypes_extensions.py index 02cee8a88..f96b064e5 100644 --- a/llama_cpp/_ctypes_extensions.py +++ b/llama_cpp/_ctypes_extensions.py @@ -60,12 +60,14 @@ def load_shared_library(lib_base_name: str, base_path: pathlib.Path): if sys.platform == "win32" and sys.version_info >= (3, 8): os.add_dll_directory(str(base_path)) - if "CUDA_PATH" in os.environ: - os.add_dll_directory(os.path.join(os.environ["CUDA_PATH"], "bin")) - os.add_dll_directory(os.path.join(os.environ["CUDA_PATH"], "lib")) - if "HIP_PATH" in os.environ: - os.add_dll_directory(os.path.join(os.environ["HIP_PATH"], "bin")) - os.add_dll_directory(os.path.join(os.environ["HIP_PATH"], "lib")) + for toolkit_env_var in ("CUDA_PATH", "HIP_PATH"): + toolkit_path = os.environ.get(toolkit_env_var) + if toolkit_path is None: + continue + for sub_dir in ("bin", "lib"): + full_path = os.path.join(toolkit_path, sub_dir) + if os.path.exists(full_path): + os.add_dll_directory(full_path) cdll_args["winmode"] = ctypes.RTLD_GLOBAL if sys.platform == "emscripten":