mikecharles
12/29/2014 - 9:52 PM

Activate and deactivate a conda environment in C Shell

Activate and deactivate a conda environment in C Shell

#!/bin/csh

# Make sure the $CONDA_ENVS_PATH env var is defined
if ( ! $?CONDA_ENVS_PATH ) then
	echo ""
	echo 'You must set the environment variable $CONDA_ENVS_PATH to point to the parent directory containing your Anaconda environments\n'
	exit 2
endif

# Make sure the $CONDA_ENVS_PATH env var isn't empty
if ( "$CONDA_ENVS_PATH" == "" ) then
	echo ""
	echo "You must set the environment variable \$CONDA_ENVS_PATH to point to the parent directory containing your Anaconda environments\n\n"
	exit 2
endif

# Make sure the $CONDA_PROD_ENV_BIN env var is defined
if ( ! $?CONDA_PROD_ENV_BIN ) then
	echo ""
	echo 'Something went wrong with your Python environment. Try opening a new terminal to start with a fresh environment.\n'
	exit 2
endif

# Make sure the $CONDA_PROD_ENV_BIN env var isn't empty
if ( "$CONDA_PROD_ENV_BIN" == "" ) then
	echo ""
	echo "Something went wrong with your Python environment. Try opening a new terminal to start with a fresh environment.\n\n"
	exit 2
endif

# Get the current python binary
set python_path=`which python`

# See if the current python binary is found under $CONDA_ENVS_PATH, exit if not
set test=`echo $python_path | awk -v test="$CONDA_ENVS_PATH" '$0 ~ test { print "MATCH" }'`
if ( $test != "MATCH" ) then
	echo "You're not currently using a conda environment"
	exit 0
endif

# Remove all occurrences of this python binary path from the $PATH
if ( $test == "MATCH" ) then
	set python_bin_dir=`echo $python_path | sed 's|/python$||'`
	setenv PATH `echo $PATH | sed -e 's|^'$python_bin_dir':||' -e 's|:'$python_bin_dir':|:|' -e 's|:'$python_bin_dir'$||'`
endif

# Add the production conda environment to the $PATH
setenv PATH ${CONDA_PROD_ENV_BIN}:${PATH}

# Get the name of the current conda environment
set conda_env=`echo $python_path | sed -e 's|^'$CONDA_ENVS_PATH'/||' -e 's|/bin/python$||'`

# Remove the name of the conda environment from the prompt
set prompt=`echo $prompt | sed 's|^('$conda_env')||'`

# Print help info
echo "Your Python environment has been reset. Here's the active version of Python:"
which python
python --version
#!/bin/csh

# Get the name of this script
set script_name = `basename $0`

# Get arguments
if ( $#argv < 1 ) then
	echo ""
	echo "Usage: source $script_name <CONDAENV>"
	exit 2
endif
set conda_env = $1

# Make sure the $CONDA_ENVS_PATH env var is defined
if ( ! $?CONDA_ENVS_PATH ) then
	echo ""
	echo 'You must set the environment variable $CONDA_ENVS_PATH to point to the parent directory containing your conda environments\n'
	echo "Usage: source $script_name <CONDAENV>"
	exit 2
endif

# Make sure the $CONDA_ENVS_PATH env var isn't empty
if ( "$CONDA_ENVS_PATH" == "" ) then
	echo ""
	echo "You must set the environment variable \$CONDA_ENVS_PATH to point to the parent directory containing your conda environments\n\n"
	echo "Usage: source $script_name <CONDAENV>"
	exit 2
endif

# See if the given Anaconda environment exists under $CONDA_ENVS_PATH
if ( ! -d "$CONDA_ENVS_PATH/$conda_env" ) then
	echo ""
	echo "The '$conda_env' conda environment was not found in $CONDA_ENVS_PATH"
	echo ""
	echo "Did you create one with 'conda create -n <myenv> python'?"
	exit 2
endif

# Remove duplicates from $PATH
set new_path = `echo $PATH | sed -e 's/$/:/;s/^/:/;s/:/::/g;:a;s#\(:[^:]\{1,\}:\)\(.*\)\1#\1\2#g;ta;s/::*/:/g;s/^://;s/:$//;'`

# Determine the active python environment
set active_python=`which python`

# If the active python environment is the production environment
set python_bin_dir=`which python | sed 's|/python$||'`
set test=`echo $active_python | awk -v test="$CONDA_ENVS_PATH" '$0 ~ test { print "MATCH" }'`
if ( $test != "MATCH" ) then
	setenv CONDA_PROD_ENV_BIN $python_bin_dir
	setenv PATH `echo $PATH | sed -e 's|^'$python_bin_dir':||' -e 's|:'$python_bin_dir':|:|' -e 's|:'$python_bin_dir'$||'`
	# Prepend the name of the conda environment to the prompt
	set prompt="($conda_env)$prompt"
# If the active python environment is a conda environment
else
	# See if this conda environment is already active
	set prev_conda_env=`which python | sed -e 's|^'$CONDA_ENVS_PATH'/||' -e 's|/bin/python$||'`
	if ( $prev_conda_env == $conda_env ) then
		echo ""
		echo "The '$conda_env' conda environment is already active"
		exit 0
	endif
	# Change the name of the conda environment in the prompt
	
	set prompt=`echo $prompt | sed 's|^('$prev_conda_env')|\('$conda_env'\)|'`
	# Remove the current conda environment from $PATH
	setenv PATH `echo $PATH | sed -e 's|^'$python_bin_dir':||' -e 's|:'$python_bin_dir':|:|' -e 's|:'$python_bin_dir'$||'`
endif

# Prepend $CONDA_ENVS_PATH/$conda_env/bin to the $PATH variable
setenv PATH $CONDA_ENVS_PATH/$conda_env/bin:$PATH

# Print help info
echo "Your Python environment has been changed to the '$conda_env' conda environment. Here's the active version of Python:"
which python
python --version
echo "To switch back to your default Python environment, type 'source deactivate.csh'"