Skip to content

Commit a73a78a

Browse files
committed
remove requirement for setuptools.pkg_resources
Fixes pydata#5676 Now depends on importlib-metadata for Python major version < 3.8
1 parent 5499949 commit a73a78a

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ python_requires = >=3.7
7878
install_requires =
7979
numpy >= 1.17
8080
pandas >= 1.0
81-
setuptools >= 40.4 # For pkg_resources
81+
importlib-metadata; python_version < '3.8'
8282

8383
[options.extras_require]
8484
io =

xarray/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import pkg_resources
2-
31
from . import testing, tutorial, ufuncs
42
from .backends.api import (
53
load_dataarray,
@@ -28,13 +26,25 @@
2826
from .core.parallel import map_blocks
2927
from .core.variable import Coordinate, IndexVariable, Variable, as_variable
3028
from .util.print_versions import show_versions
31-
3229
try:
33-
__version__ = pkg_resources.get_distribution("xarray").version
30+
try:
31+
from importlib.metadata import version, PackageNotFoundError
32+
except ImportError:
33+
try:
34+
from importlib_metadata import version, PackageNotFoundError
35+
except ImportError:
36+
raise
37+
38+
try:
39+
__version__ = version("xarray")
40+
except PackageNotFoundError:
41+
raise
42+
del version, PackageNotFoundError
3443
except Exception:
3544
# Local copy or not installed with setuptools.
3645
# Disable minimum version checks on downstream libraries.
3746
__version__ = "999"
47+
raise
3848

3949
# A hardcoded __all__ variable is necessary to appease
4050
# `mypy --strict` running in projects that import xarray.

xarray/backends/plugins.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,14 @@ def build_engines(pkg_entrypoints):
9595

9696
@functools.lru_cache(maxsize=1)
9797
def list_engines():
98-
pkg_entrypoints = pkg_resources.iter_entry_points("xarray.backends")
99-
return build_engines(pkg_entrypoints)
98+
try:
99+
from importlib.metadata import Distribution
100+
except ImportError:
101+
from importlib_metadata import Distrubtion
102+
importlib_entrypoints = (entry_point for entry_point
103+
in Distribution.from_name("xarray").entry_points
104+
if entry_point.module == "xarray.backends")
105+
return build_engines(importlib_entrypoints)
100106

101107

102108
def guess_engine(store_spec):

xarray/core/formatting_html.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from functools import lru_cache, partial
44
from html import escape
55

6-
import pkg_resources
7-
86
from .formatting import inline_variable_array_repr, short_data_repr
97
from .options import _get_boolean_with_default
108

@@ -14,10 +12,14 @@
1412
@lru_cache(None)
1513
def _load_static_files():
1614
"""Lazily load the resource files into memory the first time they are needed"""
17-
return [
18-
pkg_resources.resource_string("xarray", fname).decode("utf8")
19-
for fname in STATIC_FILES
20-
]
15+
import pathlib
16+
parent = pathlib.Path(__file__).parent / "../"
17+
result = []
18+
for fname in STATIC_FILES:
19+
with open(parent / fname) as fh:
20+
result.append(fh.read().encode("utf8"))
21+
22+
return result
2123

2224

2325
def short_data_repr_html(array):

0 commit comments

Comments
 (0)