Skip to content

Commit b3717d4

Browse files
Append versioned module path to cuda.core.__path__
1 parent 0e12b48 commit b3717d4

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

cuda_core/cuda/core/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
from cuda.core._version import __version__
66

77

8+
# TODO: remove this function altogether after wheel-variants become mainstream
89
def _import_versioned_module() -> None:
910
import importlib
11+
import pathlib
12+
import sys
1013

1114
from cuda import bindings
1215

@@ -15,13 +18,13 @@ def _import_versioned_module() -> None:
1518
raise ImportError("cuda.bindings 12.x or 13.x must be installed")
1619

1720
subdir = f"cu{cuda_major}"
18-
try:
19-
versioned_mod = importlib.import_module(f".{subdir}", __package__)
20-
# Import all symbols from the module
21-
globals().update(versioned_mod.__dict__)
22-
except ImportError:
23-
# This is not a wheel build, but a conda or local build, do nothing
24-
pass
21+
versioned_path = pathlib.Path(__file__).parent / subdir
22+
# This is a wheel build with relevant modules in cuda/core/cu<cuda_major>
23+
# directory. Let's add it to module path so imports work as expected.
24+
# cuda.core.cu<cuda_major> is not meant to behave as a module itself, and
25+
# does not belong in sys.modules
26+
if versioned_path.is_dir():
27+
__path__.append(str(versioned_path))
2528

2629

2730
_import_versioned_module()

cuda_core/tests/test_duplicate_imports.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ def test_typing_module_imports():
1616
Importing cuda.core.system should not also import cuda.core.cuXX.system
1717
"""
1818

19+
from cuda.core import * # NOQA
20+
1921
assert "cuda.core.system" in sys.modules
20-
assert f"cuda.core.cu{cuda_major}.system" not in sys.modules
22+
assert f"cuda.core.cu{cuda_major}" not in sys.modules

0 commit comments

Comments
 (0)