使用套件管理員自訂您的環境 - Amazon SageMaker

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用套件管理員自訂您的環境

使用 pip 或 conda 自訂您的環境。我們建議您使用套件管理員,而非生命週期組態指令碼。

建立和啟用您的自訂環境

本節提供您可以在 中設定環境的不同方式範例 JupyterLab。

基本 conda 環境具有 中工作流程所需的最小套件數量 SageMaker。使用下列範本建立基本 conda 環境:

# initialize conda for shell interaction conda init # create a new fresh environment conda create --name test-env # check if your new environment is created successfully conda info --envs # activate the new environment conda activate test-env # install packages in your new conda environment conda install pip boto3 pandas ipykernel # list all packages install in your new environment conda list # parse env name information from your new environment export CURRENT_ENV_NAME=$(conda info | grep "active environment" | cut -d : -f 2 | tr -d ' ') # register your new environment as Jupyter Kernel for execution python3 -m ipykernel install --user --name $CURRENT_ENV_NAME --display-name "user-env:($CURRENT_ENV_NAME)" # to exit your new environment conda deactivate

下圖顯示您已建立的環境位置。

Test-env 環境會顯示在畫面的右上角。

若要變更您的環境,請選擇它,然後從下拉式選單中選取選項。

核取記號及其對應的文字會顯示您先前建立的範例環境。

選擇選取以選取環境的核心。

使用特定 Python 版本建立 conda 環境

清理您未使用的 conda 環境有助於釋放磁碟空間並改善效能。使用下列範本清除 conda 環境:

# create a conda environment with a specific python version conda create --name py38-test-env python=3.8.10 # activate and test your new python version conda activate py38-test-env & python3 --version # Install ipykernel to facilicate env registration conda install ipykernel # parse env name information from your new environment export CURRENT_ENV_NAME=$(conda info | grep "active environment" | cut -d : -f 2 | tr -d ' ') # register your new environment as Jupyter Kernel for execution python3 -m ipykernel install --user --name $CURRENT_ENV_NAME --display-name "user-env:($CURRENT_ENV_NAME)" # deactivate your py38 test environment conda deactivate

使用特定套件集建立 conda 環境

使用下列範本建立具有特定 Python 版本和套件集的 conda 環境:

# prefill your conda environment with a set of packages, conda create --name py38-test-env python=3.8.10 pandas matplotlib=3.7 scipy ipykernel # activate your conda environment and ensure these packages exist conda activate py38-test-env # check if these packages exist conda list | grep -E 'pandas|matplotlib|scipy' # parse env name information from your new environment export CURRENT_ENV_NAME=$(conda info | grep "active environment" | cut -d : -f 2 | tr -d ' ') # register your new environment as Jupyter Kernel for execution python3 -m ipykernel install --user --name $CURRENT_ENV_NAME --display-name "user-env:($CURRENT_ENV_NAME)" # deactivate your conda environment conda deactivate

從現有環境複製 conda

複製您的 conda 環境以保留其工作狀態。您在複製的環境中進行實驗,而不必擔心在測試環境中引入中斷的變更。

使用以下命令複製環境。

# create a fresh env from a base environment conda create --name py310-base-ext --clone base # replace 'base' with another env # activate your conda environment and ensure these packages exist conda activate py310-base-ext # install ipykernel to register your env conda install ipykernel # parse env name information from your new environment export CURRENT_ENV_NAME=$(conda info | grep "active environment" | cut -d : -f 2 | tr -d ' ') # register your new environment as Jupyter Kernel for execution python3 -m ipykernel install --user --name $CURRENT_ENV_NAME --display-name "user-env:($CURRENT_ENV_NAME)" # deactivate your conda environment conda deactivate

從參考YAML檔案複製 conda

從參考YAML檔案建立 conda 環境。以下是YAML您可以使用的檔案範例。

# anatomy of a reference environment.yml name: py311-new-env channels: - conda-forge dependencies: - python=3.11 - numpy - pandas - scipy - matplotlib - pip - ipykernel - pip: - git+https://github.com/huggingface/transformers

在 下pip,我們建議只指定 Conda 不提供的相依性。

使用下列命令從 YAML 檔案建立 conda 環境。

# create your conda environment conda env create -f environment.yml # activate your env conda activate py311-new-env