diff --git a/pygmt/datasets/tile_map.py b/pygmt/datasets/tile_map.py index a60db640ce5..31f1b52cb1c 100644 --- a/pygmt/datasets/tile_map.py +++ b/pygmt/datasets/tile_map.py @@ -5,8 +5,10 @@ try: import contextily + + _HAS_CONTEXTILY = True except ImportError: - contextily = None + _HAS_CONTEXTILY = False import numpy as np import xarray as xr @@ -107,7 +109,7 @@ def load_tile_map(region, zoom="auto", source=None, lonlat=True, wait=0, max_ret * y (y) float64 -7.081e-10 -7.858e+04 ... -1.996e+07 ... * x (x) float64 -2.004e+07 -1.996e+07 ... 1.996e+07 2.004e+07 """ - if contextily is None: + if not _HAS_CONTEXTILY: raise ImportError( "Package `contextily` is required to be installed to use this function. " "Please use `python -m pip install contextily` or " diff --git a/pygmt/figure.py b/pygmt/figure.py index 170ad3b8f27..c586d96933a 100644 --- a/pygmt/figure.py +++ b/pygmt/figure.py @@ -8,8 +8,11 @@ try: import IPython + + _HAS_IPYTHON = True except ImportError: - IPython = None + _HAS_IPYTHON = False + from pygmt.clib import Session from pygmt.exceptions import GMTError, GMTInvalidInput @@ -32,7 +35,7 @@ } # Show figures in Jupyter notebooks if available -if IPython: +if _HAS_IPYTHON: get_ipython = IPython.get_ipython() if get_ipython and "IPKernelApp" in get_ipython.config: # Jupyter Notebook enabled SHOW_CONFIG["method"] = "notebook" @@ -448,7 +451,7 @@ def show(self, dpi=300, width=500, method=None, waiting=0.5, **kwargs): ) if method == "notebook": - if IPython is None: + if not _HAS_IPYTHON: raise GMTError( "Notebook display is selected, but IPython is not available. " "Make sure you have IPython installed, " diff --git a/pygmt/src/tilemap.py b/pygmt/src/tilemap.py index 7bf09e92e5e..89c0bbf468b 100644 --- a/pygmt/src/tilemap.py +++ b/pygmt/src/tilemap.py @@ -6,9 +6,11 @@ from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias try: - import rioxarray + import rioxarray # noqa: F401 + + _HAS_RIOXARRAY = True except ImportError: - rioxarray = None + _HAS_RIOXARRAY = False @fmt_docstring @@ -114,7 +116,7 @@ def tilemap( """ kwargs = self._preprocess(**kwargs) - if rioxarray is None: + if not _HAS_RIOXARRAY: 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 170bc48fac5..1401e098f9d 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -3,14 +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 -except ImportError: - IPython = None - import numpy as np import numpy.testing as npt import pytest @@ -18,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(): """ @@ -311,7 +309,7 @@ def test_figure_savefig_worldfile(): fig.savefig(fname=imgfile.name, worldfile=True) -@pytest.mark.skipif(IPython is None, 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. @@ -352,7 +350,7 @@ def test_figure_show_invalid_method(): fig.show(method="test") -@pytest.mark.skipif(IPython is not None, 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