Skip to content

Instantly share code, notes, and snippets.

@jiffyclub
Last active December 8, 2021 08:46
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save jiffyclub/9679788 to your computer and use it in GitHub Desktop.
Save jiffyclub/9679788 to your computer and use it in GitHub Desktop.
Activate and deactivate commands for working with conda environments in the fish shell. Currently assumes you are switching to a named environment, not specifying a directory.
function condalist -d 'List conda environments.'
for dir in (ls $HOME/miniconda3/envs)
echo $dir
end
end
function condactivate -d 'Activate a conda environment' -a cenv
if test -z $cenv
echo 'Usage: condactivate <env name>'
return 1
end
# condabin will be the path to the bin directory
# in the specified conda environment
set condabin $HOME/miniconda3/envs/$cenv/bin
# check whether the condabin directory actually exists and
# exit the function with an error status if it does not
if not test -d $condabin
echo 'Environment not found.'
return 1
end
# deactivate an existing conda environment if there is one
if set -q __CONDA_ENV_ACTIVE
deactivate
end
# save the current path
set -xg DEFAULT_PATH $PATH
# put the condabin directory at the front of the PATH
set -xg PATH $condabin $PATH
# this is an undocumented environmental variable that influences
# how conda behaves when you don't specify an environment for it.
# https://github.com/conda/conda/issues/473
set -xg CONDA_DEFAULT_ENV $cenv
# set up the prompt so it has the env name in it
functions -e __original_fish_prompt
functions -c fish_prompt __original_fish_prompt
function fish_prompt
set_color blue
echo -n '('$CONDA_DEFAULT_ENV') '
set_color normal
__original_fish_prompt
end
# flag for whether a conda environment has been set
set -xg __CONDA_ENV_ACTIVE 'true'
end
function deactivate -d 'Deactivate a conda environment'
if set -q __CONDA_ENV_ACTIVE
# set PATH back to its default before activating the conda env
set -xg PATH $DEFAULT_PATH
set -e DEFAULT_PATH
# unset this so that conda behaves according to its default behavior
set -e CONDA_DEFAULT_ENV
# reset to the original prompt
functions -e fish_prompt
functions -c __original_fish_prompt fish_prompt
functions -e __original_fish_prompt
set -e __CONDA_ENV_ACTIVE
end
end
# aliases so condactivate and deactivate can have shorter names
function ca -d 'Activate a conda environment'
condactivate $argv
end
function cda -d 'Deactivate a conda environment'
deactivate $argv
end
# complete conda environment names when activating
complete -c condactivate -xA -a "(condalist)"
complete -c ca -xA -a "(condalist)"
@heikkil
Copy link

heikkil commented Apr 6, 2014

Replace line 4 with the code below to make the code more generic

  if test -d $HOME/miniconda3
    set condabin $HOME/miniconda3/envs/$cenv/bin
  else if test -d $HOME/miniconda
    set condabin $HOME/miniconda/envs/$cenv/bin
  else
    set condabin $HOME/anaconda/envs/$cenv/bin
  end

@jiffyclub
Copy link
Author

Good thought, thanks!

Copy link

ghost commented May 26, 2015

This is an alternative: https://pypi.python.org/pypi/conda-workon/0.1.0

Hope you do not mind me sharing; this was the first hit on google for "conda fish".

@gitorres
Copy link

gitorres commented Jan 4, 2017

conda-workon didn't work for me, it never changes sys.prefix. I may be doing something wrong, but if you are having trouble with dependencies specified in conda not showing up in your environment.

You can go through http://conda.pydata.org/docs/troubleshooting.html#issue-conda-claims-that-a-package-is-installed-but-it-appears-not-to-be and check sys.path.

@wataash
Copy link

wataash commented Apr 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment