Skip to content

Instantly share code, notes, and snippets.

@ewiger
Forked from mangecoeur/a-conda-workon-tool.md
Last active August 29, 2015 14:04
Show Gist options
  • Save ewiger/a2253139eaed61e52761 to your computer and use it in GitHub Desktop.
Save ewiger/a2253139eaed61e52761 to your computer and use it in GitHub Desktop.

A "virtualenv activate" for Anaconda environments

I've been using the Anaconda python package from continuum.io recently and found it to be a good way to get all the complex compiled libs you need for a scientific python environment. Even better, their conda tool lets you create environments much like virtualenv, but without having to re-compile stuff like numpy, which gets old very very quickly with virtualenv and can be a nightmare to get correctly set up on OSX.

The only thing missing was an easy way to switch environments - their docs suggest running python executables from the install folder, which I find a bit of a pain. Coincidentally I came across this article - Virtualenv's bin/activate is Doing It Wrong - which desribes a simple way to launch a sub-shell with certain environment variables set. Now simple was the key word for me since my bash-fu isn't very strong, but I managed to come up with the script below. Put this in a text file called conda-workon, make it executable (chmod 777), and put it on your path somewhere (you can put it in bin, personally I just have a scripts folder in my home directory that I add to my PATH). You also need to set an environment variable ANACONDA_WORKON_HOME. In my case I put

export ANACONDA_WORKON_HOME=$HOME/anaconda/envs

in my .bash_profile

It's pretty basic and I'm sure it could be improved in many ways, but I find it useful and I hope you do too!

EDIT: The latest Anaconda allows you to create Python3 environments. This approach works fine for these environments BUT you have to remember to use python3 and ipython3 instead of python and ipython.

[Changelog]

  • Made compatible with anaconda help aggregation protocol
  • By default, ANACONDA_WORKON_HOME points to $HOME/anaconda/envs if not defined in ENV.
#!/bin/bash
# conda-workon
# A simple way to work on an Anaconda env, based on
# the inve approach described by datagrok
# https://gist.github.com/datagrok/2199506
#
# Usage:
# set an environment variable (in .profile or equivalent)
# ANACONDA_WORKON_HOME pointing to <anaconda-install>/envs
#
# Examples:
# conda-workon <env> (optional: <command>)
#
# conda-workon project-env
# launches a sub-shell with python run from project-env
#
# conda-workon project-env ipython notebook
# launches the ipython notebook from project-env
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
cat << END_OF_USAGE
usage: conda-workon [-h] ...
Switch between different anaconda environments.
optional arguments:
-h, --help show this help message and exit
END_OF_USAGE
exit
fi
if [ ! "$ANACONDA_WORKON_HOME" ]; then
ANACONDA_WORKON_HOME=$HOME/anaconda/envs
fi
CONDA_ENV="$ANACONDA_WORKON_HOME/$1"
if [ "" = "$1" ]; then
echo "must specify an env"
exit 2
fi
shift;
cd $CONDA_ENV
# Activate
export VIRTUAL_ENV="$CONDA_ENV"
export PATH="$VIRTUAL_ENV/bin:$PATH"
unset PYTHON_HOME
exec "${@:-$SHELL}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment