Skip to content

Commit

Permalink
remove pkg_resources and replace with packaging.version
Browse files Browse the repository at this point in the history
  • Loading branch information
Saelra committed Jul 20, 2024
1 parent 46e2b0e commit 77207ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
27 changes: 19 additions & 8 deletions monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
from re import match
from types import FunctionType, ModuleType
from typing import Any, Iterable, cast

import torch

import importlib.metadata
from packaging import version
from packaging.version import Version, parse


# bundle config system flags
# set MONAI_EVAL_EXPR=1 to use 'eval', default value: run_eval=True
run_eval = os.environ.get("MONAI_EVAL_EXPR", "1") != "0"
Expand Down Expand Up @@ -564,11 +568,14 @@ def version_leq(lhs: str, rhs: str) -> bool:
"""

lhs, rhs = str(lhs), str(rhs)
pkging, has_ver = optional_import("pkg_resources", name="packaging")
pkging, has_ver = optional_import("packaging", name="packaging")
# pkging, has_ver = optional_import("pkg_resources", name="packaging")
if has_ver:
try:
return cast(bool, pkging.version.Version(lhs) <= pkging.version.Version(rhs))
except pkging.version.InvalidVersion:
return cast(bool, version(lhs) <= version(rhs))
# return cast(bool, pkging.version.Version(lhs) <= pkging.version.Version(rhs))
except version.InvalidVersion:
# except pkging.version.InvalidVersion:
return True

lhs_, rhs_ = parse_version_strs(lhs, rhs)
Expand All @@ -591,11 +598,14 @@ def version_geq(lhs: str, rhs: str) -> bool:
"""
lhs, rhs = str(lhs), str(rhs)
pkging, has_ver = optional_import("pkg_resources", name="packaging")
pkging, has_ver = optional_import("packaging", name="packaging")
# pkging, has_ver = optional_import("pkg_resources", name="packaging")
if has_ver:
try:
return cast(bool, pkging.version.Version(lhs) >= pkging.version.Version(rhs))
except pkging.version.InvalidVersion:
return cast(bool, version(lhs) >= version(rhs))
# return cast(bool, pkging.version.Version(lhs) >= pkging.version.Version(rhs))
except version.InvalidVersion:
# except pkging.version.InvalidVersion:
return True

lhs_, rhs_ = parse_version_strs(lhs, rhs)
Expand Down Expand Up @@ -629,7 +639,8 @@ def pytorch_after(major: int, minor: int, patch: int = 0, current_ver_string: st
if current_ver_string is None:
_env_var = os.environ.get("PYTORCH_VER", "")
current_ver_string = _env_var if _env_var else torch.__version__
ver, has_ver = optional_import("pkg_resources", name="parse_version")
ver, has_ver = optional_import("packaging.version", name="parse")
# ver, has_ver = optional_import("pkg_resources", name="parse_version")
if has_ver:
return ver(".".join((f"{major}", f"{minor}", f"{patch}"))) <= ver(f"{current_ver_string}") # type: ignore
parts = f"{current_ver_string}".split("+", 1)[0].split(".", 3)
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import sys
import warnings

import pkg_resources
# import pkg_resources
from packaging import version
from setuptools import find_packages, setup

import versioneer
Expand All @@ -40,7 +41,8 @@

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

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

0 comments on commit 77207ad

Please sign in to comment.