You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将多个pip包安装命令添加到现有SageMaker生命周期配置中?

在SageMaker生命周期配置脚本中添加pip安装命令的位置

你需要将pip安装命令添加到脚本确定Python环境路径(PYTHON_DIR)之后,这样能确保依赖安装在与自动停止脚本一致的Python环境中,避免环境不一致问题。

以下是修改后的完整脚本示例(补全了原脚本缺失的部分,并标注了新增的pip安装代码块):

#!/bin/bash

set -ex

# OVERVIEW
# This script stops a SageMaker notebook once it's idle for more than 1 hour (default time)
# You can change the idle time for stop using the environment variable below.
# If you want the notebook the stop only if no browsers are open, remove the --ignore-connections flag
#
# Note that this script will fail if either condition is not met
#   1. Ensure the Notebook Instance has internet connectivity to fetch the example config
#   2. Ensure the Notebook Instance execution role permissions to SageMaker:StopNotebookInstance to stop the notebook 
#       and SageMaker:DescribeNotebookInstance to describe the notebook.
#
# PARAMETERS
IDLE_TIME=3600

echo "Fetching the autostop script"
wget https://raw.githubusercontent.com/aws-samples/amazon-sagemaker-notebook-instance-lifecycle-config-samples/master/scripts/auto-stop-idle/autostop.py


echo "Detecting Python install with boto3 install"

# Find which install has boto3 and use that to run the cron command. So will use default when available
# Redirect stderr as it is unneeded
CONDA_PYTHON_DIR=$(source /home/ec2-user/anaconda3/bin/activate /home/ec2-user/anaconda3/envs/JupyterSystemEnv && which python)
if $CONDA_PYTHON_DIR -c "import boto3" 2>/dev/null; then
    PYTHON_DIR=$CONDA_PYTHON_DIR
elif /usr/bin/python -c "import boto3" 2>/dev/null; then
    PYTHON_DIR='/usr/bin/python'
else
    echo "No boto3 found in Python installs. Exiting..."
    exit 1
fi

# -------------------------- 新增的pip安装命令 --------------------------
echo "Installing torch and transformers using the detected Python environment"
$PYTHON_DIR -m pip install torch transformers
# ----------------------------------------------------------------------

echo "Found Python path: $PYTHON_DIR"

# Create cron job to run the script every minute
(crontab -l 2>/dev/null; echo "* * * * * $PYTHON_DIR $PWD/autostop.py --time $IDLE_TIME --ignore-connections") | crontab -

echo "Cron job created successfully"

说明

  1. 选择这个位置的原因:脚本先完成了Python环境的检测,找到带有boto3的Python路径(与自动停止脚本共用),用$PYTHON_DIR -m pip执行安装,能保证依赖安装在正确的环境中,后续Notebook启动后可直接使用这些库。
  2. 若你希望安装到默认环境,也可将pip命令放在脚本开头(set -ex之后),但可能存在环境不一致的风险,不推荐。

内容的提问来源于stack exchange,提问作者kms

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 13:07:31