From 7d531187cbbaaadce8affbcb5ba3e23d4d65adc1 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 24 Jul 2025 11:54:29 -0700 Subject: [PATCH 1/2] Simplify cuda_bindings/site-packages/_cuda_bindings_redirector.py (as suggested by ChatGPT) to resolve https://github.com/NVIDIA/cuda-python/pull/773#issuecomment-3111270268 --- .../_cuda_bindings_redirector.py | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/cuda_bindings/site-packages/_cuda_bindings_redirector.py b/cuda_bindings/site-packages/_cuda_bindings_redirector.py index cce666aa80..578e5d002c 100644 --- a/cuda_bindings/site-packages/_cuda_bindings_redirector.py +++ b/cuda_bindings/site-packages/_cuda_bindings_redirector.py @@ -1,29 +1,9 @@ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE -import sys -from types import ModuleType +import os +import site - -class LazyCudaModule(ModuleType): - - def __getattr__(self, name): - if name == '__version__': - import warnings - warnings.warn( - "accessing cuda.__version__ is deprecated, " "please switch to use cuda.bindings.__version__ instead", - FutureWarning, - stacklevel=2, - ) - from cuda.bindings import __version__ - - return __version__ - - raise AttributeError(f"module {__name__!r} has no attribute {name!r}") - - -# Important: We need to populate the cuda namespace module first, otherwise -# we'd lose access to any of its submodules. This is a cheap op because there -# is nothing under cuda.bindings. -import cuda.bindings -sys.modules['cuda'].__class__ = LazyCudaModule +_bindings_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +if _bindings_root not in site.getsitepackages(): + site.addsitedir(_bindings_root) From 57eebd7b1d0caea9dc6583332232264c703f1dce Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 24 Jul 2025 13:53:41 -0700 Subject: [PATCH 2/2] Avoid premature import of `cuda.bindings` in redirector to fix startup error Previously, the `_cuda_bindings_redirector.py` file imported `cuda.bindings` at interpreter startup to ensure the `cuda` namespace was initialized. However, this triggered early loading of Cython modules that depend on `win32api`, which may not be available yet due to `.pth` file processing order. This commit replaces `import cuda.bindings` with `import cuda`, which is sufficient to ensure the `cuda` namespace is in `sys.modules`. The version redirect via `cuda.__version__` is preserved by assigning a custom `LazyCudaModule` type to `sys.modules["cuda"]`. As a result, `cuda.__version__` continues to work (with a deprecation warning), while avoiding startup errors when `win32api` is not yet importable. --- .../_cuda_bindings_redirector.py | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/cuda_bindings/site-packages/_cuda_bindings_redirector.py b/cuda_bindings/site-packages/_cuda_bindings_redirector.py index 578e5d002c..13b3c04cf1 100644 --- a/cuda_bindings/site-packages/_cuda_bindings_redirector.py +++ b/cuda_bindings/site-packages/_cuda_bindings_redirector.py @@ -1,9 +1,30 @@ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE -import os -import site +import sys +from types import ModuleType -_bindings_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) -if _bindings_root not in site.getsitepackages(): - site.addsitedir(_bindings_root) + +# Make sure 'cuda' is importable as a namespace package +import cuda + + +class LazyCudaModule(ModuleType): + + def __getattr__(self, name): + if name == '__version__': + import warnings + warnings.warn( + "accessing cuda.__version__ is deprecated, " "please switch to use cuda.bindings.__version__ instead", + FutureWarning, + stacklevel=2, + ) + from cuda.bindings import __version__ + + return __version__ + + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + + +# Patch in LazyCudaModule for `cuda` +sys.modules['cuda'].__class__ = LazyCudaModule