From deff1e9c241757971149832ba366346e39e5f21a Mon Sep 17 00:00:00 2001 From: Vladislav Tumko <56307628+vectorvp@users.noreply.github.com> Date: Tue, 26 Nov 2024 20:31:46 +0700 Subject: [PATCH] feat: print packages to install --- monai/data/image_writer.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/monai/data/image_writer.py b/monai/data/image_writer.py index b9e8b9e68e..93cd82c0be 100644 --- a/monai/data/image_writer.py +++ b/monai/data/image_writer.py @@ -11,6 +11,7 @@ from __future__ import annotations +import re from collections.abc import Mapping, Sequence from typing import TYPE_CHECKING, Any, cast @@ -106,17 +107,23 @@ def resolve_writer(ext_name, error_if_not_found=True) -> Sequence: if fmt.startswith("."): fmt = fmt[1:] avail_writers = [] + required_pkg = [] default_writers = SUPPORTED_WRITERS.get(EXT_WILDCARD, ()) for _writer in look_up_option(fmt, SUPPORTED_WRITERS, default=default_writers): try: _writer() # this triggers `monai.utils.module.require_pkg` to check the system availability avail_writers.append(_writer) - except OptionalImportError: + except OptionalImportError as e: + error_match = re.search(r"`(.*?)`", str(e)) + if error_match: + required_pkg.append(error_match.group(1)) continue except Exception: # other writer init errors indicating it exists avail_writers.append(_writer) if not avail_writers and error_if_not_found: - raise OptionalImportError(f"No ImageWriter backend found for {fmt}.") + raise OptionalImportError( + f"No ImageWriter backend found for {fmt}, install one of the required packages in {required_pkg}." + ) writer_tuple = ensure_tuple(avail_writers) SUPPORTED_WRITERS[fmt] = writer_tuple return writer_tuple