diff --git a/libtbx_refresh.py b/libtbx_refresh.py index f80786519..eef867e0f 100644 --- a/libtbx_refresh.py +++ b/libtbx_refresh.py @@ -1,6 +1,7 @@ from __future__ import annotations import contextlib +import importlib import inspect import io import os @@ -17,11 +18,6 @@ except ModuleNotFoundError: pass -try: - import pkg_resources -except ModuleNotFoundError: - pkg_resources = None - def _install_setup_readonly_fallback(package_name: str): """ @@ -67,9 +63,8 @@ def _install_setup_readonly_fallback(package_name: str): if import_path not in sys.path: sys.path.insert(0, import_path) - # ...and add to the existing pkg_resources working_set - if pkg_resources: - pkg_resources.working_set.add_entry(import_path) + # ...and make sure it is picked up by the import system + importlib.invalidate_caches() # This is already generated by this point, but will get picked up # on the second libtbx.refresh. diff --git a/newsfragments/XXX.misc b/newsfragments/XXX.misc new file mode 100644 index 000000000..0780b3d13 --- /dev/null +++ b/newsfragments/XXX.misc @@ -0,0 +1 @@ +Move from deprecated pkg_resources to importlib. diff --git a/src/dxtbx/format/Registry.py b/src/dxtbx/format/Registry.py index b7034a22e..9f4d74b75 100644 --- a/src/dxtbx/format/Registry.py +++ b/src/dxtbx/format/Registry.py @@ -5,12 +5,11 @@ """ from __future__ import annotations +import importlib.metadata import os import typing from typing import Callable -import pkg_resources - from dxtbx.util import get_url_scheme if typing.TYPE_CHECKING: @@ -35,7 +34,7 @@ def get_format_class_index() -> dict[str, tuple[Callable[[], type[Format]], list """ if not hasattr(get_format_class_index, "cache"): class_index = {} - for e in pkg_resources.iter_entry_points("dxtbx.format"): + for e in importlib.metadata.entry_points(group="dxtbx.format"): if ":" in e.name: format_name, base_classes_str = e.name.split(":", 1) base_classes = tuple(base_classes_str.split(",")) diff --git a/src/dxtbx/model/experiment_list.py b/src/dxtbx/model/experiment_list.py index 9770129ed..4b40cb311 100644 --- a/src/dxtbx/model/experiment_list.py +++ b/src/dxtbx/model/experiment_list.py @@ -3,6 +3,7 @@ import collections import copy import errno +import importlib.metadata import itertools import json import logging @@ -11,8 +12,6 @@ import pickle from typing import Any, Callable, Generator, Iterable -import pkg_resources - import dxtbx.datablock from dxtbx.datablock import ( BeamComparison, @@ -467,7 +466,9 @@ def _lookup_model(self, name, experiment_dict): @staticmethod def _scaling_model_from_dict(obj): """Get the scaling model from a dictionary.""" - for entry_point in pkg_resources.iter_entry_points("dxtbx.scaling_model_ext"): + for entry_point in importlib.metadata.entry_points( + group="dxtbx.scaling_model_ext" + ): if entry_point.name == obj["__id__"]: return entry_point.load().from_dict(obj) diff --git a/src/dxtbx/model/profile.py b/src/dxtbx/model/profile.py index 0fbb0434b..b452b0bd3 100644 --- a/src/dxtbx/model/profile.py +++ b/src/dxtbx/model/profile.py @@ -1,9 +1,8 @@ from __future__ import annotations +import importlib.metadata import logging -import pkg_resources - class ProfileModelFactory: """ @@ -17,7 +16,7 @@ def from_dict(obj): """ if obj is None: return None - for entry_point in pkg_resources.iter_entry_points("dxtbx.profile_model"): + for entry_point in importlib.metadata.entry_points(group="dxtbx.profile_model"): if entry_point.name == obj["__id__"]: return entry_point.load().from_dict(obj) logging.getLogger("dxtbx.model.profile").warn(