Skip to content

Improve the way to import optional modules #2809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 4, 2023
6 changes: 4 additions & 2 deletions pygmt/datasets/tile_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand Down
9 changes: 6 additions & 3 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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, "
Expand Down
8 changes: 5 additions & 3 deletions pygmt/src/tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "
Expand Down
12 changes: 5 additions & 7 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@

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
from pygmt import Figure, set_display
from pygmt.exceptions import GMTError, GMTInvalidInput
from pygmt.helpers import GMTTempFile

HAS_IPYTHON = bool(importlib.util.find_spec("IPython"))


def test_figure_region():
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down