Skip to content

Commit 5558310

Browse files
Max Jonesandersy005
andauthored
Use informative error message for optional dependency imports (#118)
* Use informative error message for optional dependency imports * Apply suggestions from code review Co-authored-by: Anderson Banihirwe <[email protected]>
1 parent 77c470b commit 5558310

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

doc/index.rst

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,41 @@ Installation
1212

1313
Xbatcher can be installed from PyPI as::
1414

15-
pip install xbatcher
15+
python -m pip install xbatcher
1616

1717
Or via Conda as::
1818

1919
conda install -c conda-forge xbatcher
2020

2121
Or from source as::
2222

23-
pip install git+https://github.com/xarray-contrib/xbatcher.git
23+
python -m pip install git+https://github.com/xarray-contrib/xbatcher.git
24+
25+
Optional Dependencies
26+
~~~~~~~~~~~~~~~~~~~~~
27+
28+
.. note::
29+
The required dependencies installed with Xbatcher are `Xarray <https://xarray.dev/>`_,
30+
`Dask <https://www.dask.org/>`_, and `NumPy <https://numpy.org/>`_.
31+
You will need to separately install `TensorFlow <https://www.tensorflow.org/>`_
32+
or `PyTorch <https://pytorch.org/>`_ to use those data loaders or
33+
Xarray accessors.
34+
35+
To install Xbatcher and PyTorch via `Conda <https://docs.conda.io/>`_::
36+
37+
conda install -c conda-forge xbatcher pytorch
38+
39+
Or via PyPI::
40+
41+
python -m pip install xbatcher[torch]
42+
43+
To install Xbatcher and TensorFlow via `Conda <https://docs.conda.io/>`_::
44+
45+
conda install -c conda-forge xbatcher tensorflow
46+
47+
Or via PyPI::
48+
49+
python -m pip install xbatcher[tensorflow]
2450

2551
Basic Usage
2652
-----------

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fallback_version = "999"
5858

5959
[tool.isort]
6060
profile = 'black'
61-
known_third_party = ["numpy", "pytest", "setuptools", "sphinx_autosummary_accessors", "tensorflow", "torch", "xarray"]
61+
known_third_party = ["numpy", "pytest", "setuptools", "sphinx_autosummary_accessors", "torch", "xarray"]
6262

6363
[tool.pytest.ini_options]
6464
log_cli = true

xbatcher/loaders/keras.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from typing import Any, Callable, Optional, Tuple
22

3-
import tensorflow as tf
3+
try:
4+
import tensorflow as tf
5+
except ImportError as exc:
6+
raise ImportError(
7+
"The Xbatcher TensorFlow Dataset API depends on TensorFlow. Please "
8+
"install TensorFlow to proceed."
9+
) from exc
410

511
# Notes:
612
# This module includes one Keras dataset, which can be provided to model.fit().

xbatcher/loaders/torch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from typing import Any, Callable, Optional, Tuple
22

3-
import torch
3+
try:
4+
import torch
5+
except ImportError as exc:
6+
raise ImportError(
7+
"The Xbatcher PyTorch Dataset API depends on PyTorch. Please "
8+
"install PyTorch to proceed."
9+
) from exc
410

511
# Notes:
612
# This module includes two PyTorch datasets.

0 commit comments

Comments
 (0)