From a73a78a167df7e9c0257e168dba2ffc65534becf Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Thu, 7 Oct 2021 21:54:42 +0200 Subject: [PATCH] remove requirement for setuptools.pkg_resources Fixes #5676 Now depends on importlib-metadata for Python major version < 3.8 --- setup.cfg | 2 +- xarray/__init__.py | 18 ++++++++++++++---- xarray/backends/plugins.py | 10 ++++++++-- xarray/core/formatting_html.py | 14 ++++++++------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/setup.cfg b/setup.cfg index aa8ca8df0ff..4860704d77d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -78,7 +78,7 @@ python_requires = >=3.7 install_requires = numpy >= 1.17 pandas >= 1.0 - setuptools >= 40.4 # For pkg_resources + importlib-metadata; python_version < '3.8' [options.extras_require] io = diff --git a/xarray/__init__.py b/xarray/__init__.py index eb35bbb2d18..3a04bfa8631 100644 --- a/xarray/__init__.py +++ b/xarray/__init__.py @@ -1,5 +1,3 @@ -import pkg_resources - from . import testing, tutorial, ufuncs from .backends.api import ( load_dataarray, @@ -28,13 +26,25 @@ from .core.parallel import map_blocks from .core.variable import Coordinate, IndexVariable, Variable, as_variable from .util.print_versions import show_versions - try: - __version__ = pkg_resources.get_distribution("xarray").version + try: + from importlib.metadata import version, PackageNotFoundError + except ImportError: + try: + from importlib_metadata import version, PackageNotFoundError + except ImportError: + raise + + try: + __version__ = version("xarray") + except PackageNotFoundError: + raise + del version, PackageNotFoundError except Exception: # Local copy or not installed with setuptools. # Disable minimum version checks on downstream libraries. __version__ = "999" + raise # A hardcoded __all__ variable is necessary to appease # `mypy --strict` running in projects that import xarray. diff --git a/xarray/backends/plugins.py b/xarray/backends/plugins.py index 08c1bec8325..7e4894c00e1 100644 --- a/xarray/backends/plugins.py +++ b/xarray/backends/plugins.py @@ -95,8 +95,14 @@ def build_engines(pkg_entrypoints): @functools.lru_cache(maxsize=1) def list_engines(): - pkg_entrypoints = pkg_resources.iter_entry_points("xarray.backends") - return build_engines(pkg_entrypoints) + try: + from importlib.metadata import Distribution + except ImportError: + from importlib_metadata import Distrubtion + importlib_entrypoints = (entry_point for entry_point + in Distribution.from_name("xarray").entry_points + if entry_point.module == "xarray.backends") + return build_engines(importlib_entrypoints) def guess_engine(store_spec): diff --git a/xarray/core/formatting_html.py b/xarray/core/formatting_html.py index 2a480427d4e..ad642ddd06b 100644 --- a/xarray/core/formatting_html.py +++ b/xarray/core/formatting_html.py @@ -3,8 +3,6 @@ from functools import lru_cache, partial from html import escape -import pkg_resources - from .formatting import inline_variable_array_repr, short_data_repr from .options import _get_boolean_with_default @@ -14,10 +12,14 @@ @lru_cache(None) def _load_static_files(): """Lazily load the resource files into memory the first time they are needed""" - return [ - pkg_resources.resource_string("xarray", fname).decode("utf8") - for fname in STATIC_FILES - ] + import pathlib + parent = pathlib.Path(__file__).parent / "../" + result = [] + for fname in STATIC_FILES: + with open(parent / fname) as fh: + result.append(fh.read().encode("utf8")) + + return result def short_data_repr_html(array):