Skip to content

Commit c8be4a6

Browse files
committed
fix: install sagemaker-core in Spark container pre-execution
The Spark processing image does not have sagemaker-core pre-installed, causing spark_app.py to fail with ModuleNotFoundError when importing sagemaker.core.remote_function. Add sagemaker-core to the pre-execution commands when spark_config is set.
1 parent 0cf35eb commit c8be4a6

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

  • sagemaker-core/src/sagemaker/core/remote_function

‎sagemaker-core/src/sagemaker/core/remote_function/job.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,23 @@ def __init__(
670670
sagemaker_session=self.sagemaker_session,
671671
)
672672

673-
# When using Spark, ensure sagemaker-feature-store-pyspark is installed
674-
# and its version-matched JAR is on Spark's classpath before spark-submit
673+
# When using Spark, ensure sagemaker-core and sagemaker-feature-store-pyspark
674+
# are installed and the version-matched JAR is on Spark's classpath before spark-submit.
675+
# The Spark processing image does not include sagemaker-core, so it must be installed
676+
# explicitly for spark_app.py to import sagemaker.core.remote_function.
675677
if spark_config:
678+
sagemaker_pysdk_version = (
679+
RuntimeEnvironmentManager()._current_sagemaker_pysdk_version()
680+
)
681+
if sagemaker_pysdk_version and not sagemaker_pysdk_version.endswith(".dev0"):
682+
install_sagemaker_cmd = (
683+
f"pip install --root-user-action=ignore"
684+
f" 'sagemaker-core=={sagemaker_pysdk_version}'"
685+
)
686+
else:
687+
install_sagemaker_cmd = (
688+
"pip install --root-user-action=ignore 'sagemaker-core'"
689+
)
676690
install_cmd = (
677691
"pip install --root-user-action=ignore"
678692
" 'sagemaker-feature-store-pyspark>=2,<3'"
@@ -690,10 +704,13 @@ def __init__(
690704
"for j in glob.glob(os.path.join(jars_dir, '*' + spark_ver + '*.jar'))]\""
691705
)
692706
if self.pre_execution_commands is None:
693-
self.pre_execution_commands = [install_cmd, copy_jar_cmd]
694-
elif install_cmd not in self.pre_execution_commands:
695-
self.pre_execution_commands.append(install_cmd)
696-
self.pre_execution_commands.append(copy_jar_cmd)
707+
self.pre_execution_commands = [install_sagemaker_cmd, install_cmd, copy_jar_cmd]
708+
else:
709+
if install_sagemaker_cmd not in self.pre_execution_commands:
710+
self.pre_execution_commands.insert(0, install_sagemaker_cmd)
711+
if install_cmd not in self.pre_execution_commands:
712+
self.pre_execution_commands.append(install_cmd)
713+
self.pre_execution_commands.append(copy_jar_cmd)
697714

698715
self.pre_execution_script = resolve_value_from_config(
699716
direct_input=pre_execution_script,

0 commit comments

Comments
 (0)