Skip to content

Commit

Permalink
remove requirement for setuptools.pkg_resources
Browse files Browse the repository at this point in the history
Fixes pydata#5676

Now depends on importlib-metadata for Python major version < 3.8
  • Loading branch information
marscher committed Oct 7, 2021
1 parent 5499949 commit a73a78a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
18 changes: 14 additions & 4 deletions xarray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pkg_resources

from . import testing, tutorial, ufuncs
from .backends.api import (
load_dataarray,
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions xarray/backends/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 8 additions & 6 deletions xarray/core/formatting_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down

0 comments on commit a73a78a

Please sign in to comment.