22
33import logging
44from typing import Dict , Optional , Union , cast
5+ from databricks .sdk .errors import DatabricksError
56
67logger = logging .getLogger ('databricks.sdk' )
78is_local_implementation = True
@@ -73,6 +74,19 @@ def inner() -> Dict[str, str]:
7374 return None , None
7475
7576
77+ def _is_unexpected_exception_loading_user_namespace (e : Exception ) -> bool :
78+ # The dbruntime module is not present outside of DBR
79+ if isinstance (e , ImportError ):
80+ return False
81+ # In notebooks, the UserNamespaceInitializer works, but the notebook context is not propagated to
82+ # spawned Python subprocesses, resulting in this class throwing an
83+ # pyspark.errors.exceptions.base.PySparkRuntimeError. The SDK does not depend on PySpark, so we
84+ # need to check the type and module name directly.
85+ if type (e ).__name__ == 'PySparkRuntimeError' and e .__module__ == 'pyspark.errors.exceptions.base' :
86+ return False
87+ return True
88+
89+
7690try :
7791 # Internal implementation
7892 # Separated from above for backward compatibility
@@ -85,7 +99,9 @@ def inner() -> Dict[str, str]:
8599 continue
86100 _globals [var ] = userNamespaceGlobals [var ]
87101 is_local_implementation = False
88- except ImportError :
102+ except Exception as e :
103+ if _is_unexpected_exception_loading_user_namespace (e ):
104+ raise DatabricksError (f"Failed to initialize runtime globals" ) from e
89105 # OSS implementation
90106 is_local_implementation = True
91107
0 commit comments