activate-conda-env-in-shell

Aroma

最核心的方法是

1
2
3
#!/bin/bash
eval "$(conda shell.bash hook)"
conda activate <your_conda_env>

加入错误处理、信息提示,我们可以得到鲁棒性好的版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash

TARGET_ENV="target_env"

if ! command -v conda &> /dev/null; then
echo "error: conda not found"
exit 1
fi
eval "$(conda shell.bash hook)"

if [ "$CONDA_DEFAULT_ENV" != "$TARGET_ENV" ]; then
echo "activate: $TARGET_ENV"
conda activate "$TARGET_ENV"
else
echo "already in env: $TARGET_ENV"
fi

python test.py

额外阅读

  • Title: activate-conda-env-in-shell
  • Author: Aroma
  • Created at : 2025-10-06 00:00:00
  • Updated at : 2025-10-06 00:00:00
  • Link: https://recynie.github.io/blog/2025-10-06/activate-conda-env-in-shell/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
activate-conda-env-in-shell