

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

# 使用套件管理員自訂您的環境
<a name="studio-updated-jl-user-guide-customize-package-manager"></a>

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

## 建立和啟用您的自訂環境
<a name="studio-updated-jl-create-basic-conda"></a>

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

基本 conda 環境具有 SageMaker AI 中工作流程所需的套件數量下限。使用下列範本建立基本 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 環境會顯示在畫面的右上角。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/juptyer-notebook-environment-location.png)


若要變更您的環境，請選擇它，然後從下拉式功能表中選取選項。

![\[核取記號及其對應文字會顯示您先前建立的範例環境。\]](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/jupyter-notebook-select-env.png)


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

## 使用特定 Python 版本建立 conda 環境
<a name="studio-updated-jl-create-conda-version"></a>

清除您未使用的 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 環境
<a name="studio-updated-jl-create-conda-specific-packages"></a>

使用下列範本搭配特定 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
<a name="studio-updated-jl-create-conda-clone"></a>

複製您的 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
<a name="studio-updated-jl-create-conda-yaml"></a>

從參考 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
```