Created
September 25, 2019 17:16
-
-
Save raeidsaqur/2362deb11226c99647d83e2ad9bf920d to your computer and use it in GitHub Desktop.
conda-auto-env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# conda-auto-env automatically activates 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. | |
# | |
# To install add this line to your .bashrc or .bash-profile: | |
# | |
# source /path/to/conda_auto_env_remote.sh | |
# | |
# conda-auto-env also supports remote anaconda.org environments. | |
# To specify a remote environment create an environment-remote.yml | |
# file with the name and channel of your environment | |
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 | |
source activate $ENV | |
if [ $? -eq 0 ]; then | |
: | |
else | |
# Create the environment and activate | |
echo "Conda env '$ENV' doesn't exist." | |
conda env create -q | |
source activate $ENV | |
fi | |
fi | |
fi | |
if [ -e "environment-remote.yml" ]; then | |
# 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 | |
source activate $ENV | |
if [ $? -eq 0 ]; then | |
: | |
else | |
# 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 | |
} | |
export PROMPT_COMMAND=conda_auto_env_remote |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment