Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
# conda-auto-env

Automatically activate a conda environment when entering a folder with an environment.yml file.
(Semi-)Automatically activate a conda environment when entering a folder with an `environment.yml` file.

If the environment doesn't exist, conda-auto-env creates it and activates it for you.
If the environment doesn't exist, `conda-auto-env` creates it and activates it for you.

This functionality was inspired by [conda auto activate](https://github.com/sotte/conda_auto_activate), [virtualenv auto activate](https://gist.github.com/garyjohnson/394c58e22a2adfa103e2) and [autoenv](https://github.com/kennethreitz/autoenv).

## Install

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

source /path/to/conda_auto_env.sh
```bash
source /path/to/conda_auto_env.sh
```

If you also want this script to be ran automatically, whenever you enter a directory add also the following:

```bash
export PROMPT_COMMAND="conda_auto_env;$PROMPT_COMMAND"
```

Otherwise, whenever in a directory containing `environment.yml`, you can execute `conda_auto_env`.

### Remote environments

Expand Down
70 changes: 55 additions & 15 deletions conda_auto_env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# conda-auto-env automatically activates a conda environment when
# entering a folder with an environment.yml file.
# conda-auto-env is a utility to activate a conda environment in a folder
# with an environment.yml file or based on the current dir's name.
#
# If the environment doesn't exist, conda-auto-env creates it and
# activates it for you.
Expand All @@ -11,24 +11,64 @@
# source /path/to/conda_auto_env.sh
#

DEFAULT_PY_VER=3.6

function conda_auto_env() {

function activate_env() {
conda activate $1
if [[ $? -eq 0 ]]; then
echo "${1} activated successfully"
else
echo "Failed to activate ${1}"
return 1
fi
}

function deactivate_env() {
conda deactivate
if [[ $? -eq 0 ]]; then
echo "Deactivated successfully"
else
echo "Failed to deactivate"
return 1
fi
}

# Determine ENV's name; eitehr from environment.yml
# or based on current dir name
if [ -e "environment.yml" ]; then
# echo "environment.yml file found"
echo "Found environment.yml"
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."
else
echo "No environment.yml found. Reverting to current dir"
ENV=${PWD##*/}
fi

echo "Processing the environment: ${ENV}"

if [ $ENV = $CONDA_DEFAULT_ENV ]; then
# ENV is already activated. Deactivate
deactivate_env
elif [[ $PATH != *$ENV* ]]; then
# ENV is not activated. Trying to activate
activate_env $ENV
if [ $? -ne 0 ]; then
# Failed to activate. Creating environment
echo "Conda env '$ENV' doesn't exist."
if [[ -e "environment.yml" ]]; then
echo "Creating ${ENV} based on environment.yml"
conda env create -q
source activate $ENV
else
echo "Creating ${ENV} with default settings"
conda create -n ${ENV} python=${DEFAULT_PY_VER}
fi
activate_env $ENV
fi
fi
}

export PROMPT_COMMAND=conda_auto_env
# Make sure utility functions are local
unset -f activate_env
unset -f deactivate_env
unset ENV
}