From 5995df45fcfc12969b381b6761d693efe2f2229b Mon Sep 17 00:00:00 2001 From: Cenny Wenner Date: Sat, 11 Jun 2016 16:47:07 +0200 Subject: [PATCH 1/9] Specify check of active environment [fix] More accurate check of whether an environment is already active. Environments could previously be seen as active by certain spurious directory or environment names. --- conda_auto_env.sh | 2 +- conda_auto_env_remote.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 2470c54..dbfebfe 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -16,7 +16,7 @@ function conda_auto_env() { # 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 + if [[ $PATH != */envs/*$ENV*/* ]]; then # Check if the environment exists source activate $ENV if [ $? -eq 0 ]; then diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index f24b3c0..eb7d19c 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -19,7 +19,7 @@ function conda_auto_env_remote() { # 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 + if [[ $PATH != */envs/*$ENV*/* ]]; then # Check if the environment exists source activate $ENV if [ $? -eq 0 ]; then @@ -37,7 +37,7 @@ function conda_auto_env_remote() { 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 + if [[ $PATH != */envs/*$ENV*/* ]]; then # Check if the environment exists source activate $ENV if [ $? -eq 0 ]; then From 1d1cc07a750418974b4a33c6825e2441f7c1aaaf Mon Sep 17 00:00:00 2001 From: Cenny Wenner Date: Sat, 11 Jun 2016 17:02:22 +0200 Subject: [PATCH 2/9] Deactivate when leaving environment directories [feat] Environment deactivated in favor of the user's default conda environment when no longer in a subdirectory of the directory in which the active environment was activated. --- conda_auto_env.sh | 10 +++++++++- conda_auto_env_remote.sh | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index dbfebfe..e93dcf5 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -17,8 +17,10 @@ function conda_auto_env() { ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') # Check if you are already in the environment if [[ $PATH != */envs/*$ENV*/* ]]; then - # Check if the environment exists + # Attempt to activate environment + CONDA_ENVIRONMENT_ROOT="" #For spawned shells source activate $ENV + CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -eq 0 ]; then : else @@ -28,6 +30,12 @@ function conda_auto_env() { source activate $ENV fi fi + elif [[ $PATH = */envs/* ]]\ + && [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT ]]\ + && [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT/* ]] + then + CONDA_ENVIRONMENT_ROOT="" + source deactivate fi } diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index eb7d19c..6681952 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -20,8 +20,10 @@ function conda_auto_env_remote() { ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') # Check if you are already in the environment if [[ $PATH != */envs/*$ENV*/* ]]; then - # Check if the environment exists + # Attempt to activate environment + CONDA_ENVIRONMENT_ROOT="" #For spawned shells source activate $ENV + CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -eq 0 ]; then : else @@ -38,8 +40,10 @@ function conda_auto_env_remote() { CHANNEL=$(sed -n '2p' environment-remote.yml | cut -f2 -d ' ') # Check if you are already in the environment if [[ $PATH != */envs/*$ENV*/* ]]; then - # Check if the environment exists + # Attempt to activate environment + CONDA_ENVIRONMENT_ROOT="" #For spawned shells source activate $ENV + CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -eq 0 ]; then : else @@ -51,6 +55,13 @@ function conda_auto_env_remote() { fi fi fi + 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 From d725a34eb52ea63afd948330fc8adf6fe8712b41 Mon Sep 17 00:00:00 2001 From: Cenny Wenner Date: Sat, 11 Jun 2016 17:32:15 +0200 Subject: [PATCH 3/9] Clarify comments [clean] Slightly reworded or added comments for clarity. --- conda_auto_env.sh | 10 ++++++---- conda_auto_env_remote.sh | 17 ++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index e93dcf5..2738d6e 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -15,21 +15,23 @@ 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 + # Check if the environment is already active. if [[ $PATH != */envs/*$ENV*/* ]]; then - # Attempt to activate environment + # Attempt to activate environment. CONDA_ENVIRONMENT_ROOT="" #For spawned shells source activate $ENV + # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -eq 0 ]; then : else - # Create the environment and activate - echo "Conda env '$ENV' doesn't exist." + # Create the environment and activate. + echo "Conda environment '$ENV' doesn't exist: Creating." conda env create -q source 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/* ]] diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index 6681952..ebe1fd2 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -18,17 +18,18 @@ 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 + # Check if the environment is already active. if [[ $PATH != */envs/*$ENV*/* ]]; then - # Attempt to activate environment + # Attempt to activate environment. CONDA_ENVIRONMENT_ROOT="" #For spawned shells source activate $ENV + # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -eq 0 ]; then : else - # Create the environment and activate - echo "Conda env '$ENV' doesn't exist." + # Create the environment and activate. + echo "Conda environment '$ENV' doesn't exist: Creating." conda env create -q source activate $ENV fi @@ -38,16 +39,17 @@ 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 + # Check if the environment is already active. if [[ $PATH != */envs/*$ENV*/* ]]; then - # Attempt to activate environment + # Attempt to activate environment. CONDA_ENVIRONMENT_ROOT="" #For spawned shells source activate $ENV + # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -eq 0 ]; then : else - # Create the environment and activate + # Create the environment and activate. echo "Conda env '$ENV' doesn't exist." REMOTE=$CHANNEL'/'$ENV conda env create $REMOTE -q @@ -55,6 +57,7 @@ function conda_auto_env_remote() { 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/* ]] From 073382ac0f5e2b8ea3894a1d628e47c7fab6f37d Mon Sep 17 00:00:00 2001 From: Cenny Wenner Date: Sat, 11 Jun 2016 17:37:22 +0200 Subject: [PATCH 4/9] Simplify conditional tests [clean] Slightly simplified expression: "-ne 0" for "-eq 0 .. else". --- conda_auto_env.sh | 4 +--- conda_auto_env_remote.sh | 8 ++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 2738d6e..83c3651 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -22,9 +22,7 @@ function conda_auto_env() { source activate $ENV # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" - if [ $? -eq 0 ]; then - : - else + if [ $? -ne 0 ]; then # Create the environment and activate. echo "Conda environment '$ENV' doesn't exist: Creating." conda env create -q diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index ebe1fd2..0529e2c 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -25,9 +25,7 @@ function conda_auto_env_remote() { source activate $ENV # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" - if [ $? -eq 0 ]; then - : - else + if [ $? -ne 0 ]; then # Create the environment and activate. echo "Conda environment '$ENV' doesn't exist: Creating." conda env create -q @@ -46,9 +44,7 @@ function conda_auto_env_remote() { source activate $ENV # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" - if [ $? -eq 0 ]; then - : - else + if [ $? -ne 0 ]; then # Create the environment and activate. echo "Conda env '$ENV' doesn't exist." REMOTE=$CHANNEL'/'$ENV From 71106e6967f8f7761031d7348a954668efb1e1db Mon Sep 17 00:00:00 2001 From: Cenny Wenner Date: Sat, 11 Jun 2016 17:56:04 +0200 Subject: [PATCH 5/9] Support zshell [feat] Support for sourcing in both bash and zsh without any extraneous commands. Kudos: birdsarah. --- README.md | 2 +- conda_auto_env.sh | 16 ++++++++++++++-- conda_auto_env_remote.sh | 14 +++++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d5c2326..d5e86cf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 83c3651..d7d7356 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -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 # @@ -39,4 +39,16 @@ function conda_auto_env() { fi } -export PROMPT_COMMAND=conda_auto_env +# 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 diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index 0529e2c..64a4589 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -63,4 +63,16 @@ function conda_auto_env_remote() { 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 From de9c1028b3a923d4df1b36bf75c0c85f1e20df55 Mon Sep 17 00:00:00 2001 From: Cenny Date: Wed, 28 Aug 2019 11:34:13 +0200 Subject: [PATCH 6/9] Update to use conda command instead of source --- conda_auto_env.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index d7d7356..996569e 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -19,14 +19,14 @@ function conda_auto_env() { if [[ $PATH != */envs/*$ENV*/* ]]; then # Attempt to activate environment. CONDA_ENVIRONMENT_ROOT="" #For spawned shells - source activate $ENV + 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. @@ -35,7 +35,7 @@ function conda_auto_env() { && [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT/* ]] then CONDA_ENVIRONMENT_ROOT="" - source deactivate + conda deactivate fi } From b3579c04ad528018a51532bd5b90354792f74b52 Mon Sep 17 00:00:00 2001 From: introkun Date: Mon, 25 Jan 2021 22:28:51 +0300 Subject: [PATCH 7/9] fixed script to run on big sur --- conda_auto_env.sh | 2 +- conda_auto_env_remote.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index 996569e..dc52f7d 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -40,7 +40,7 @@ function conda_auto_env() { } # Check active shell. -if [[ $(ps -p$$ -ocmd=) == "zsh" ]]; then +if [[ $(ps -p$$ -ocommand=) == "-zsh" ]]; then # For zsh, use the chpwd hook. autoload -U add-zsh-hook add-zsh-hook chpwd conda_auto_env diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index 64a4589..e334d51 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -64,7 +64,7 @@ function conda_auto_env_remote() { } # Check active shell. -if [[ $(ps -p$$ -ocmd=) == "zsh" ]]; then +if [[ $(ps -p$$ -ocommand=) == "-zsh" ]]; then # For zsh, use the chpwd hook. autoload -U add-zsh-hook add-zsh-hook chpwd conda_auto_env From 3e072c56f2a339774d0de5d90fb9cb025c68e19a Mon Sep 17 00:00:00 2001 From: introkun Date: Fri, 29 Jan 2021 23:31:39 +0300 Subject: [PATCH 8/9] fixed script when exiting from env folder --- conda_auto_env.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/conda_auto_env.sh b/conda_auto_env.sh index dc52f7d..63aaec3 100644 --- a/conda_auto_env.sh +++ b/conda_auto_env.sh @@ -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, .bash-profile, or .zshrc: +# To install add this line to your .bashrc or .bash-profile: # # source /path/to/conda_auto_env.sh # @@ -15,12 +15,12 @@ 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 the environment is already active. - if [[ $PATH != */envs/*$ENV*/* ]]; then - # Attempt to activate environment. + # echo "Check if the environment is already active." + if [[ $PATH != */envs/$ENV/* ]]; then + # echo "Attempt to activate environment." CONDA_ENVIRONMENT_ROOT="" #For spawned shells conda activate $ENV - # Set root directory of active environment. + # echo "Set root directory of active environment." CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -ne 0 ]; then # Create the environment and activate. @@ -29,13 +29,15 @@ function conda_auto_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/* ]] + elif [[ $PATH == */envs/* ]]\ + && ([[ $(pwd) != $CONDA_ENVIRONMENT_ROOT ]]\ + || [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT/* ]]) then + # echo "Deactivate active environment since we are no longer among its subdirectories." CONDA_ENVIRONMENT_ROOT="" + # echo "Before conda deactivate" conda deactivate + # echo "After conda deactivate" fi } From 708b1be987538a1617119984deca26697d22032b Mon Sep 17 00:00:00 2001 From: introkun Date: Fri, 29 Jan 2021 23:35:34 +0300 Subject: [PATCH 9/9] fixed remote script --- conda_auto_env_remote.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/conda_auto_env_remote.sh b/conda_auto_env_remote.sh index e334d51..743189e 100644 --- a/conda_auto_env_remote.sh +++ b/conda_auto_env_remote.sh @@ -19,17 +19,17 @@ function conda_auto_env_remote() { # echo "environment.yml file found" ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') # Check if the environment is already active. - if [[ $PATH != */envs/*$ENV*/* ]]; then + if [[ $PATH != */envs/$ENV/* ]]; then # Attempt to activate environment. CONDA_ENVIRONMENT_ROOT="" #For spawned shells - source activate $ENV + 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 fi @@ -38,10 +38,10 @@ function conda_auto_env_remote() { ENV=$(sed -n '1p' environment-remote.yml | cut -f2 -d ' ') CHANNEL=$(sed -n '2p' environment-remote.yml | cut -f2 -d ' ') # Check if the environment is already active. - if [[ $PATH != */envs/*$ENV*/* ]]; then + if [[ $PATH != */envs/$ENV/* ]]; then # Attempt to activate environment. CONDA_ENVIRONMENT_ROOT="" #For spawned shells - source activate $ENV + conda activate $ENV # Set root directory of active environment. CONDA_ENVIRONMENT_ROOT="$(pwd)" if [ $? -ne 0 ]; then @@ -49,17 +49,17 @@ function conda_auto_env_remote() { echo "Conda env '$ENV' doesn't exist." REMOTE=$CHANNEL'/'$ENV conda env create $REMOTE -q - source activate $ENV + conda 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/* ]] + && ([[ $(pwd) != $CONDA_ENVIRONMENT_ROOT ]]\ + || [[ $(pwd) != $CONDA_ENVIRONMENT_ROOT/* ]]) then CONDA_ENVIRONMENT_ROOT="" - source deactivate + conda deactivate fi }