Skip to content

Commit 77207ad

Browse files
committed
remove pkg_resources and replace with packaging.version
1 parent 46e2b0e commit 77207ad

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

monai/utils/module.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
from re import match
2828
from types import FunctionType, ModuleType
2929
from typing import Any, Iterable, cast
30-
3130
import torch
3231

32+
import importlib.metadata
33+
from packaging import version
34+
from packaging.version import Version, parse
35+
36+
3337
# bundle config system flags
3438
# set MONAI_EVAL_EXPR=1 to use 'eval', default value: run_eval=True
3539
run_eval = os.environ.get("MONAI_EVAL_EXPR", "1") != "0"
@@ -564,11 +568,14 @@ def version_leq(lhs: str, rhs: str) -> bool:
564568
"""
565569

566570
lhs, rhs = str(lhs), str(rhs)
567-
pkging, has_ver = optional_import("pkg_resources", name="packaging")
571+
pkging, has_ver = optional_import("packaging", name="packaging")
572+
# pkging, has_ver = optional_import("pkg_resources", name="packaging")
568573
if has_ver:
569574
try:
570-
return cast(bool, pkging.version.Version(lhs) <= pkging.version.Version(rhs))
571-
except pkging.version.InvalidVersion:
575+
return cast(bool, version(lhs) <= version(rhs))
576+
# return cast(bool, pkging.version.Version(lhs) <= pkging.version.Version(rhs))
577+
except version.InvalidVersion:
578+
# except pkging.version.InvalidVersion:
572579
return True
573580

574581
lhs_, rhs_ = parse_version_strs(lhs, rhs)
@@ -591,11 +598,14 @@ def version_geq(lhs: str, rhs: str) -> bool:
591598
592599
"""
593600
lhs, rhs = str(lhs), str(rhs)
594-
pkging, has_ver = optional_import("pkg_resources", name="packaging")
601+
pkging, has_ver = optional_import("packaging", name="packaging")
602+
# pkging, has_ver = optional_import("pkg_resources", name="packaging")
595603
if has_ver:
596604
try:
597-
return cast(bool, pkging.version.Version(lhs) >= pkging.version.Version(rhs))
598-
except pkging.version.InvalidVersion:
605+
return cast(bool, version(lhs) >= version(rhs))
606+
# return cast(bool, pkging.version.Version(lhs) >= pkging.version.Version(rhs))
607+
except version.InvalidVersion:
608+
# except pkging.version.InvalidVersion:
599609
return True
600610

601611
lhs_, rhs_ = parse_version_strs(lhs, rhs)
@@ -629,7 +639,8 @@ def pytorch_after(major: int, minor: int, patch: int = 0, current_ver_string: st
629639
if current_ver_string is None:
630640
_env_var = os.environ.get("PYTORCH_VER", "")
631641
current_ver_string = _env_var if _env_var else torch.__version__
632-
ver, has_ver = optional_import("pkg_resources", name="parse_version")
642+
ver, has_ver = optional_import("packaging.version", name="parse")
643+
# ver, has_ver = optional_import("pkg_resources", name="parse_version")
633644
if has_ver:
634645
return ver(".".join((f"{major}", f"{minor}", f"{patch}"))) <= ver(f"{current_ver_string}") # type: ignore
635646
parts = f"{current_ver_string}".split("+", 1)[0].split(".", 3)

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import sys
1818
import warnings
1919

20-
import pkg_resources
20+
# import pkg_resources
21+
from packaging import version
2122
from setuptools import find_packages, setup
2223

2324
import versioneer
@@ -40,7 +41,8 @@
4041

4142
BUILD_CUDA = FORCE_CUDA or (torch.cuda.is_available() and (CUDA_HOME is not None))
4243

43-
_pt_version = pkg_resources.parse_version(torch.__version__).release
44+
# _pt_version = pkg_resources.parse_version(torch.__version__).release
45+
_pt_version = version.parse(torch.__version__).release
4446
if _pt_version is None or len(_pt_version) < 3:
4547
raise AssertionError("unknown torch version")
4648
TORCH_VERSION = int(_pt_version[0]) * 10000 + int(_pt_version[1]) * 100 + int(_pt_version[2])

0 commit comments

Comments
 (0)