Skip to content
Merged
13 changes: 8 additions & 5 deletions google/cloud/managed_spark_connect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
# limitations under the License.
import importlib.metadata
import importlib.util
import sys
import warnings


def _in_ipython() -> bool:
# Not environment.is_interactive(), which is also true in a plain
# `python -i` REPL, where the [interactive] extras do nothing.
ip_mod = sys.modules.get("IPython")
if ip_mod is None:
return False
try:
from IPython import get_ipython

return get_ipython() is not None
return ip_mod.get_ipython() is not None
except Exception:
return False

Expand Down Expand Up @@ -97,8 +99,9 @@ def _check_pyspark_version(pyspark_version: str) -> None:

# traitlets, like every other dependency of the interactive extras, only comes
# with the [interactive] extra. IPython depends on it, so without it there can
# be no shell and nothing to initialize.
if importlib.util.find_spec("traitlets") is not None:
# be no shell and nothing to initialize. Skip importing `_ipython` when not
# running inside an active IPython shell.
if _in_ipython() and importlib.util.find_spec("traitlets") is not None:
# Only _init_extras: the ManagedSparkConnect configurable is looked up by
# class name (`c.ManagedSparkConnect...`), so it need not be importable
# from here, and would otherwise exist only when traitlets is installed.
Expand Down
5 changes: 4 additions & 1 deletion google/cloud/managed_spark_connect/_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@ def _init_extras() -> None:
"""Initialize interactive extras on package import unless opted out."""
ip = None
try:
from IPython import get_ipython
ip_mod = sys.modules.get("IPython")
get_ipython = getattr(ip_mod, "get_ipython", None)
if not callable(get_ipython):
return

ip = get_ipython()
if ip is None:
Expand Down
Loading
Loading