diff --git a/pygmt/src/tilemap.py b/pygmt/src/tilemap.py index 95f87d68093..55c27881003 100644 --- a/pygmt/src/tilemap.py +++ b/pygmt/src/tilemap.py @@ -1,17 +1,12 @@ """ tilemap - Plot XYZ tile maps. """ +import importlib + from pygmt.clib import Session from pygmt.datasets.tile_map import load_tile_map from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias -try: - import rioxarray - - _has_rioxarray = True -except ImportError: - _has_rioxarray = False - @fmt_docstring @use_alias( @@ -116,7 +111,7 @@ def tilemap( """ kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access - if not _has_rioxarray: + if importlib.util.find_spec("rioxarray") is None: raise ImportError( "Package `rioxarray` is required to be installed to use this function. " "Please use `python -m pip install rioxarray` or " diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index ed94080c4d8..8136ee12f6b 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -3,17 +3,10 @@ Doesn't include the plotting commands which have their own test files. """ +import importlib import os from pathlib import Path -try: - import IPython - - _has_ipython = True -except ImportError: - _has_ipython = False - - import numpy as np import numpy.testing as npt import pytest @@ -21,6 +14,8 @@ from pygmt.exceptions import GMTError, GMTInvalidInput from pygmt.helpers import GMTTempFile +HAS_IPYTHON = bool(importlib.util.find_spec("IPython")) + def test_figure_region(): """ @@ -315,7 +310,7 @@ def test_figure_savefig_worldfile(): fig.savefig(fname=imgfile.name, worldfile=True) -@pytest.mark.skipif(not _has_ipython, reason="run when IPython is installed") +@pytest.mark.skipif(not HAS_IPYTHON, reason="run when IPython is installed") def test_figure_show(): """ Test that show creates the correct file name and deletes the temp dir. @@ -356,7 +351,7 @@ def test_figure_show_invalid_method(): fig.show(method="test") -@pytest.mark.skipif(_has_ipython, reason="run without IPython installed") +@pytest.mark.skipif(HAS_IPYTHON, reason="run without IPython installed") def test_figure_show_notebook_error_without_ipython(): """ Test to check if an error is raised when display method is 'notebook', but