Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support zsh and deactivate when leaving environment directories #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This functionality was inspired by [conda auto activate](https://github.com/sott

## Install

To install add this line to your .bashrc or .bash-profile:
To install add this line to your .bashrc, .bash-profile, or .zshrc:

source /path/to/conda_auto_env.sh

Expand Down
44 changes: 32 additions & 12 deletions conda_auto_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# If the environment doesn't exist, conda-auto-env creates it and
# activates it for you.
#
# To install add this line to your .bashrc or .bash-profile:
# To install add this line to your .bashrc, .bash-profile, or .zshrc:
#
# source /path/to/conda_auto_env.sh
#
Expand All @@ -15,20 +15,40 @@ function conda_auto_env() {
if [ -e "environment.yml" ]; then
# echo "environment.yml file found"
ENV=$(head -n 1 environment.yml | cut -f2 -d ' ')
# Check if you are already in the environment
if [[ $PATH != *$ENV* ]]; then
# Check if the environment exists
source activate $ENV
if [ $? -eq 0 ]; then
:
else
# Create the environment and activate
echo "Conda env '$ENV' doesn't exist."
# Check if the environment is already active.
if [[ $PATH != */envs/*$ENV*/* ]]; then
# Attempt to activate environment.
CONDA_ENVIRONMENT_ROOT="" #For spawned shells
conda activate $ENV
# Set root directory of active environment.
CONDA_ENVIRONMENT_ROOT="$(pwd)"
if [ $? -ne 0 ]; then
# Create the environment and activate.
echo "Conda environment '$ENV' doesn't exist: Creating."
conda env create -q
source activate $ENV
conda activate $ENV
fi
fi
# Deactivate active environment if we are no longer among its subdirectories.
elif [[ $PATH = */envs/* ]]\
&& [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT ]]\
&& [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT/* ]]
then
CONDA_ENVIRONMENT_ROOT=""
conda deactivate
fi
}

export PROMPT_COMMAND=conda_auto_env
# Check active shell.
if [[ $(ps -p$$ -ocmd=) == "zsh" ]]; then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$(ps -p$$ -ocommand=) == "-zsh"
only this worked for me
OS Big Sur, zsh

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# For zsh, use the chpwd hook.
autoload -U add-zsh-hook
add-zsh-hook chpwd conda_auto_env
# Run for present directory as it does not fire the above hook.
conda_auto_env
# More aggressive option in case the above hook misses some use case:
#precmd() { conda_auto_env; }
else
# For bash, no hooks and we rely on the env. var. PROMPT_COMMAND:
export PROMPT_COMMAND=conda_auto_env
fi
54 changes: 38 additions & 16 deletions conda_auto_env_remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ function conda_auto_env_remote() {
if [ -e "environment.yml" ]; then
# echo "environment.yml file found"
ENV=$(head -n 1 environment.yml | cut -f2 -d ' ')
# Check if you are already in the environment
if [[ $PATH != *$ENV* ]]; then
# Check if the environment exists
# Check if the environment is already active.
if [[ $PATH != */envs/*$ENV*/* ]]; then
# Attempt to activate environment.
CONDA_ENVIRONMENT_ROOT="" #For spawned shells
source activate $ENV
if [ $? -eq 0 ]; then
:
else
# Create the environment and activate
echo "Conda env '$ENV' doesn't exist."
# Set root directory of active environment.
CONDA_ENVIRONMENT_ROOT="$(pwd)"
if [ $? -ne 0 ]; then
# Create the environment and activate.
echo "Conda environment '$ENV' doesn't exist: Creating."
conda env create -q
source activate $ENV
fi
Expand All @@ -36,21 +37,42 @@ function conda_auto_env_remote() {
# echo "environment.yml file found"
ENV=$(sed -n '1p' environment-remote.yml | cut -f2 -d ' ')
CHANNEL=$(sed -n '2p' environment-remote.yml | cut -f2 -d ' ')
# Check if you are already in the environment
if [[ $PATH != *$ENV* ]]; then
# Check if the environment exists
# Check if the environment is already active.
if [[ $PATH != */envs/*$ENV*/* ]]; then
# Attempt to activate environment.
CONDA_ENVIRONMENT_ROOT="" #For spawned shells
source activate $ENV
if [ $? -eq 0 ]; then
:
else
# Create the environment and activate
# Set root directory of active environment.
CONDA_ENVIRONMENT_ROOT="$(pwd)"
if [ $? -ne 0 ]; then
# Create the environment and activate.
echo "Conda env '$ENV' doesn't exist."
REMOTE=$CHANNEL'/'$ENV
conda env create $REMOTE -q
source activate $ENV
fi
fi
fi
# Deactivate active environment if we are no longer among its subdirectories.
if [[ $PATH = */envs/* ]]\
&& [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT ]]\
&& [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT/* ]]
then
CONDA_ENVIRONMENT_ROOT=""
source deactivate
fi
}

export PROMPT_COMMAND=conda_auto_env_remote
# Check active shell.
if [[ $(ps -p$$ -ocmd=) == "zsh" ]]; then
# For zsh, use the chpwd hook.
autoload -U add-zsh-hook
add-zsh-hook chpwd conda_auto_env
# Run for present directory as it does not fire the above hook.
conda_auto_env
# More aggressive option in case the above hook misses some use case:
#precmd() { conda_auto_env; }
else
# For bash, no hooks and we rely on the env. var. PROMPT_COMMAND:
export PROMPT_COMMAND=conda_auto_env
fi