diff --git a/python/pyspark/errors/utils.py b/python/pyspark/errors/utils.py index 9155bfb54abe8..89721d23c3858 100644 --- a/python/pyspark/errors/utils.py +++ b/python/pyspark/errors/utils.py @@ -175,9 +175,20 @@ def _capture_call_site(spark_session: "SparkSession", depth: int) -> str: # We try import here since IPython is not a required dependency try: - from IPython import get_ipython - - ipython = get_ipython() + import IPython + + # ipykernel is required for IPython + import ipykernel # type: ignore[import-not-found] + + ipython = IPython.get_ipython() + # Filtering out IPython related frames + ipy_root = os.path.dirname(IPython.__file__) + ipykernel_root = os.path.dirname(ipykernel.__file__) + selected_frames = [ + frame + for frame in selected_frames + if (ipy_root not in frame.filename) and (ipykernel_root not in frame.filename) + ] except ImportError: ipython = None