diff --git a/box/box.py b/box/box.py index 322b313..a6b2222 100644 --- a/box/box.py +++ b/box/box.py @@ -18,12 +18,6 @@ except ImportError: from collections.abc import Callable, Iterable, Mapping -try: - from IPython import get_ipython -except ImportError: - ipython = False -else: - ipython = True if get_ipython() else False import box from box.converters import ( @@ -56,6 +50,17 @@ NO_NAMESPACE = object() +def _is_ipython(): + try: + from IPython import get_ipython + except ImportError: + ipython = False + else: + ipython = True if get_ipython() else False + + return ipython + + def _exception_cause(e): """ Unwrap BoxKeyError and BoxValueError errors to their cause. @@ -483,7 +488,7 @@ def __setstate__(self, state): self.__dict__.update(state) def __get_default(self, item, attr=False): - if ipython and item in ("getdoc", "shape"): + if item in ("getdoc", "shape") and _is_ipython(): return None default_value = self._box_config["default_box_attr"] if default_value in (self._box_config["box_class"], dict):