Skip to content

Commit 6c2a6e7

Browse files
authored
Merge pull request #1564 from rgaiacs/improve-activate-conda-script
Improve activate-conda.sh to not kill shell
2 parents 6dd5154 + d3bc560 commit 6c2a6e7

1 file changed

Lines changed: 38 additions & 32 deletions

File tree

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
set -e
2-
3-
# Setup conda
41
CONDA_PROFILE="${CONDA_DIR}/etc/profile.d/conda.sh"
5-
echo "Activating profile: ${CONDA_PROFILE}"
6-
test -f $CONDA_PROFILE && . $CONDA_PROFILE
72

8-
# Setup micromamba
9-
eval $(micromamba shell hook -s posix -r ${CONDA_DIR})
3+
if [ -f $CONDA_PROFILE ]; then
4+
# Setup conda
5+
echo "Activating profile: ${CONDA_PROFILE}"
6+
source $CONDA_PROFILE
107

11-
# Setup mamba
12-
export MAMBA_ROOT_PREFIX="${CONDA_DIR}"
13-
__mamba_setup="$("${CONDA_DIR}/bin/mamba" shell hook --shell posix 2> /dev/null)"
14-
if [ $? -eq 0 ]; then
15-
eval "$__mamba_setup"
16-
else
17-
alias mamba="${CONDA_DIR}/bin/mamba" # Fallback on help from mamba activate
18-
fi
19-
unset __mamba_setup
8+
# Setup micromamba
9+
eval $(micromamba shell hook -s posix -r ${CONDA_DIR})
2010

21-
# Activate the environment
22-
if [[ "${KERNEL_PYTHON_PREFIX}" != "${NB_PYTHON_PREFIX}" ]]; then
23-
# if the kernel is a separate env, stack them
24-
# so both are on PATH, notebook first
25-
mamba activate ${KERNEL_PYTHON_PREFIX}
26-
mamba activate --stack ${NB_PYTHON_PREFIX}
11+
# Setup mamba
12+
export MAMBA_ROOT_PREFIX="${CONDA_DIR}"
13+
__mamba_setup="$("${CONDA_DIR}/bin/mamba" shell hook --shell posix 2> /dev/null)"
14+
if [ $? -eq 0 ]; then
15+
eval "$__mamba_setup"
16+
else
17+
alias mamba="${CONDA_DIR}/bin/mamba" # Fallback on help from mamba activate
18+
fi
19+
unset __mamba_setup
2720

28-
# even though it's second on $PATH
29-
# make sure CONDA_DEFAULT_ENV is the *kernel* env
30-
# so that `!conda install PKG` installs in the kernel env
31-
# where user packages are installed, not the notebook env
32-
# which only contains UI when the two are different
33-
export CONDA_DEFAULT_ENV="${KERNEL_PYTHON_PREFIX}"
34-
else
35-
mamba activate ${NB_PYTHON_PREFIX}
36-
fi
21+
# Activate the environment
22+
if [[ "${KERNEL_PYTHON_PREFIX}" != "${NB_PYTHON_PREFIX}" ]]; then
23+
# if the kernel is a separate env, stack them
24+
# so both are on PATH, notebook first
25+
mamba activate ${KERNEL_PYTHON_PREFIX}
26+
mamba activate --stack ${NB_PYTHON_PREFIX}
3727

38-
set +e
28+
# even though it's second on $PATH
29+
# make sure CONDA_DEFAULT_ENV is the *kernel* env
30+
# so that `!conda install PKG` installs in the kernel env
31+
# where user packages are installed, not the notebook env
32+
# which only contains UI when the two are different
33+
export CONDA_DEFAULT_ENV="${KERNEL_PYTHON_PREFIX}"
34+
else
35+
mamba activate ${NB_PYTHON_PREFIX}
36+
fi
37+
38+
else
39+
echo "Conda compatible environment not loaded due to"
40+
echo
41+
echo "- Mising file ${CONDA_PROFILE}"
42+
echo
43+
echo "Some tools might NOT be available!"
44+
fi

0 commit comments

Comments
 (0)