Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions monai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def filter(self, record):
# *.so files and folder "_C" may not exist when the cpp extensions are not compiled
excludes = "|".join(
[
"(.*(nvimgcodec_pydicom_plugin)$)",
"(^(monai.handlers))",
"(^(monai.bundle))",
"(^(monai.fl))",
Expand Down
20 changes: 20 additions & 0 deletions tests/data/test_init_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
from __future__ import annotations

import os
import subprocess
import sys
import tempfile
import unittest
from importlib.machinery import PathFinder

import numpy as np

Expand Down Expand Up @@ -65,6 +68,23 @@ def test_load_image(self):
inst = LoadImaged("image", reader="NvImgCodecPydicomReader")
self.assertIsInstance(inst, LoadImaged)

def test_nvimgcodec_not_imported_on_monai_import(self):
"""Verify importing MONAI does not eagerly load the optional NVIDIA package."""
nvidia_spec = PathFinder.find_spec("nvidia")
if nvidia_spec is None or nvidia_spec.submodule_search_locations is None:
self.skipTest("nvidia.nvimgcodec is not installed")

nvimgcodec_spec = PathFinder.find_spec("nvimgcodec", nvidia_spec.submodule_search_locations)
if nvimgcodec_spec is None:
self.skipTest("nvidia.nvimgcodec is not installed")

code = "import monai, sys; print('nvidia.nvimgcodec' in sys.modules)"
result = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True)

self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(result.stdout.strip(), "False")
self.assertEqual(result.stderr, "")

@SkipIfNoModule("nibabel")
@SkipIfNoModule("cupy")
@SkipIfNoModule("kvikio")
Expand Down
Loading