Skip to content

Commit

Permalink
Replaced package "pkg_resources" with "packaging" (#7953)
Browse files Browse the repository at this point in the history
Fixes #7559 .

### Description  
Replaced "pkg_resources" references with "packaging" in
MONAI/monai/utils/module.py & setup.py
Changes were made in functions "pytorch_after", "version_leq",
"version_geq".

### Types of changes
- Non-breaking change (fix or new feature that would not break existing
functionality).

---------

Signed-off-by: dedeepyasai <dedeepyasai.sai@gmail.com>
Signed-off-by: saelra <rasaelee@gmail.com>
Signed-off-by: Kelvin R <kelvinrbNsc@gmail.com>
Signed-off-by: ken-ni <kennett.vera@gmail.com>
Signed-off-by: Dureti <98233210+DuretiShemsi@users.noreply.github.com>
Signed-off-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: saelra <rasaelee@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: dedeepyasai <dedeepyasai.sai@gmail.com>
Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
Co-authored-by: Ratanachat Saelee <146144408+Saelra@users.noreply.github.com>
Co-authored-by: ken-ni <kennett.vera@gmail.com>
Co-authored-by: Dureti <98233210+DuretiShemsi@users.noreply.github.com>
  • Loading branch information
8 people authored Aug 9, 2024
1 parent 069519d commit 6be7b13
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ jobs:
- name: Install dependencies
run: |
find /opt/hostedtoolcache/* -maxdepth 0 ! -name 'Python' -exec rm -rf {} \;
python -m pip install --user --upgrade pip setuptools wheel twine
python -m pip install --user --upgrade pip setuptools wheel twine packaging
# install the latest pytorch for testing
# however, "pip install monai*.tar.gz" will build cpp/cuda with an isolated
# fresh torch installation according to pyproject.toml
Expand Down
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ onnxruntime; python_version <= '3.10'
zarr
huggingface_hub
pyamg>=5.0.0
packaging
7 changes: 4 additions & 3 deletions monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ 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.Version")
if has_ver:
try:
return cast(bool, pkging.version.Version(lhs) <= pkging.version.Version(rhs))
Expand All @@ -591,7 +591,8 @@ 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.Version")

if has_ver:
try:
return cast(bool, pkging.version.Version(lhs) >= pkging.version.Version(rhs))
Expand Down Expand Up @@ -629,7 +630,7 @@ 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")
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
1 change: 1 addition & 0 deletions requirements-min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ 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
packaging
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ pyyaml =
pyyaml
fire =
fire
packaging =
packaging
jsonschema =
jsonschema
pynrrd =
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sys
import warnings

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

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

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 = 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 6be7b13

Please sign in to comment.