From 87862f0d5730d42d282e779fc1450f18b4869863 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Tue, 2 Jul 2024 09:02:05 -0500 Subject: [PATCH 1/2] Remove use of deprecated python 3.12 strtobool distutils is not available in python 3.12, so a substitute is needed for the strtobool code. Use same resolution as implemented by the ITK project. Signed-off-by: Hans Johnson --- monai/utils/misc.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/monai/utils/misc.py b/monai/utils/misc.py index ab9fe259aa..96a59e1b35 100644 --- a/monai/utils/misc.py +++ b/monai/utils/misc.py @@ -24,7 +24,6 @@ import warnings from ast import literal_eval from collections.abc import Callable, Iterable, Sequence -from distutils.util import strtobool from math import log10 from pathlib import Path from typing import TYPE_CHECKING, Any, TypeVar, cast, overload @@ -78,6 +77,25 @@ "run_cmd", ] + +def _strtobool(val: str) -> bool: + """ + Replaces deprecated (pre python 3.12) + distutils strtobool function. + + True values are y, yes, t, true, on and 1; + False values are n, no, f, false, off and 0. + Raises ValueError if val is anything else. + """ + val = val.lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return True + elif val in ("n", "no", "f", "false", "off", "0"): + return False + else: + raise ValueError(f"invalid truth value {val}") + + _seed = None _flag_deterministic = torch.backends.cudnn.deterministic _flag_cudnn_benchmark = torch.backends.cudnn.benchmark @@ -400,7 +418,7 @@ def _parse_var(s): d[key] = literal_eval(value) except ValueError: try: - d[key] = bool(strtobool(str(value))) + d[key] = bool(_strtobool(str(value))) except ValueError: d[key] = value return d From 20aae2364e73bc86b18e4f3648adc0fa0df9d973 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Tue, 2 Jul 2024 15:51:07 -0500 Subject: [PATCH 2/2] Fix optional import error. Due to the removal of the long-deprecated pkgutil.ImpImporter class a newer version of setuptools that provides this capability. for Python 3.12. This resolves: monai.utils.module.OptionalImportError: Please install mlflow before using MLFlowHandler. (module 'pkgutil' has no attribute 'ImpImporter'). Signed-off-by: Hans Johnson --- requirements-min.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements-min.txt b/requirements-min.txt index ad0bb1ef20..a091ef0568 100644 --- a/requirements-min.txt +++ b/requirements-min.txt @@ -1,5 +1,6 @@ # Requirements for minimal tests -r requirements.txt -setuptools>=50.3.0,<66.0.0,!=60.6.0 +setuptools>=50.3.0,<66.0.0,!=60.6.0 ; python_version < "3.12" +setuptools>=70.2.0; python_version >= "3.12" coverage>=5.5 parameterized