├── README.md ├── conda_auto_env.sh ├── LICENSE └── conda_auto_env_remote.sh /README.md: -------------------------------------------------------------------------------- 1 | # conda-auto-env 2 | 3 | Automatically activate a conda environment when entering a folder with an environment.yml file. 4 | 5 | If the environment doesn't exist, conda-auto-env creates it and activates it for you. 6 | 7 | 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). 8 | 9 | ## Install 10 | 11 | To install add this line to your .bashrc or .bash-profile: 12 | 13 | source /path/to/conda_auto_env.sh 14 | 15 | ### Remote environments 16 | 17 | Alternatively, if you would also like to have support remote anaconda.org environments. Change those instructions to ``source /path/to/conda_auto_env_remote.sh``. To specify a remote environment create an ``environment-remote.yml`` file with the name and channel of your environment: 18 | 19 | ```yaml 20 | name: pyladies-bokeh 21 | channel: chdoig 22 | ``` 23 | -------------------------------------------------------------------------------- /conda_auto_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # conda-auto-env automatically activates a conda environment when 4 | # entering a folder with an environment.yml file. 5 | # 6 | # If the environment doesn't exist, conda-auto-env creates it and 7 | # activates it for you. 8 | # 9 | # To install add this line to your .bashrc or .bash-profile: 10 | # 11 | # source /path/to/conda_auto_env.sh 12 | # 13 | 14 | function conda_auto_env() { 15 | if [ -e "environment.yml" ]; then 16 | # echo "environment.yml file found" 17 | ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') 18 | # Check if you are already in the environment 19 | if [[ $PATH != *$ENV* ]]; then 20 | # Check if the environment exists 21 | source activate $ENV 22 | if [ $? -eq 0 ]; then 23 | : 24 | else 25 | # Create the environment and activate 26 | echo "Conda env '$ENV' doesn't exist." 27 | conda env create -q 28 | source activate $ENV 29 | fi 30 | fi 31 | fi 32 | } 33 | 34 | export PROMPT_COMMAND=conda_auto_env 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (c) 2015 Continuum Analytics, Inc. / http://continuum.io 2 | All Rights Reserved 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of Continuum Analytics, Inc. nor the 12 | names of its contributors may be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL CONTINUUM ANALYTICS BE LIABLE FOR ANY 19 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /conda_auto_env_remote.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # conda-auto-env automatically activates a conda environment when 4 | # entering a folder with an environment.yml file. 5 | # 6 | # If the environment doesn't exist, conda-auto-env creates it and 7 | # activates it for you. 8 | # 9 | # To install add this line to your .bashrc or .bash-profile: 10 | # 11 | # source /path/to/conda_auto_env_remote.sh 12 | # 13 | # conda-auto-env also supports remote anaconda.org environments. 14 | # To specify a remote environment create an environment-remote.yml 15 | # file with the name and channel of your environment 16 | 17 | function conda_auto_env_remote() { 18 | if [ -e "environment.yml" ]; then 19 | # echo "environment.yml file found" 20 | ENV=$(head -n 1 environment.yml | cut -f2 -d ' ') 21 | # Check if you are already in the environment 22 | if [[ $PATH != *$ENV* ]]; then 23 | # Check if the environment exists 24 | source activate $ENV 25 | if [ $? -eq 0 ]; then 26 | : 27 | else 28 | # Create the environment and activate 29 | echo "Conda env '$ENV' doesn't exist." 30 | conda env create -q 31 | source activate $ENV 32 | fi 33 | fi 34 | fi 35 | if [ -e "environment-remote.yml" ]; then 36 | # echo "environment.yml file found" 37 | ENV=$(sed -n '1p' environment-remote.yml | cut -f2 -d ' ') 38 | CHANNEL=$(sed -n '2p' environment-remote.yml | cut -f2 -d ' ') 39 | # Check if you are already in the environment 40 | if [[ $PATH != *$ENV* ]]; then 41 | # Check if the environment exists 42 | source activate $ENV 43 | if [ $? -eq 0 ]; then 44 | : 45 | else 46 | # Create the environment and activate 47 | echo "Conda env '$ENV' doesn't exist." 48 | REMOTE=$CHANNEL'/'$ENV 49 | conda env create $REMOTE -q 50 | source activate $ENV 51 | fi 52 | fi 53 | fi 54 | } 55 | 56 | export PROMPT_COMMAND=conda_auto_env_remote 57 | --------------------------------------------------------------------------------