bullet-train.zsh icon indicating copy to clipboard operation
bullet-train.zsh copied to clipboard

anaconda virtualenv doesn't  display properly

Open karterotte opened this issue 7 years ago • 6 comments
trafficstars

the virtual env name doesn't display in powerline but like this:

11:28:59  XXX@XXX  ~  <----it's powerline $ vim /etc/bashrc (test_env)

I want to know how to fix it ,THANKS!

karterotte avatar Jun 28 '18 03:06 karterotte

screen shot 2018-08-30 at 19 52 15 Same issue here. When I source virtual environments, 'system' turns into the env name but not for conda environments. How to fix this? Thank you

ferreirafabio avatar Aug 30 '18 17:08 ferreirafabio

Looks like current implementation doesn't support conda environments.

It could be modified to support conda by using CONDA_DEFAULT_ENV to get environment name. Also you've to disable prompt set by conda (shows up before the theme surrounded with brackets) with

conda config --set changeps1 False

salmanulfarzy avatar Aug 31 '18 04:08 salmanulfarzy

I modified the theme file ~/.oh-my-zsh/custom/themes/bullet-train.zsh-theme in the following three steps. Please make sure you are not using virtualenv to manage your python virtual env, otherwise you may be confused by two different descriptions of the python enviroment.

  1. add one line in the scope of BULLETTRAIN_PROMPT_ORDER.
# Define order and content of prompt
if [ ! -n "${BULLETTRAIN_PROMPT_ORDER+1}" ]; then
  BULLETTRAIN_PROMPT_ORDER=(
    time
    status
    custom
    context
    dir
    screen
    perl
    ruby
    virtualenv
    conda # <-- ADD 
    nvm
    aws
    go
    rust
    elixir
    git
    hg
    cmd_exec_time
  )
fi
  1. add the related promot function prompt_conda() under prompt_virtualenv()
# Virtualenv: current working virtualenv
prompt_virtualenv() {
  local virtualenv_path="$VIRTUAL_ENV"
  if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
    prompt_segment $BULLETTRAIN_VIRTUALENV_BG $BULLETTRAIN_VIRTUALENV_FG $BULLETTRAIN_VIRTUALENV_PREFIX" $(basename $virtualenv_path)"
  elif which pyenv &> /dev/null; then
    prompt_segment $BULLETTRAIN_VIRTUALENV_BG $BULLETTRAIN_VIRTUALENV_FG $BULLETTRAIN_VIRTUALENV_PREFIX" $(pyenv version | sed -e 's/ (set.*$//' | tr '\n' ' ' | sed 's/.$//')"
  fi
}

# Add following lines
# CONDA
prompt_conda() {
  prompt_segment $BULLETTRAIN_VIRTUALENV_BG $BULLETTRAIN_VIRTUALENV_FG $BULLETTRAIN_VIRTUALENV_PREFIX" $(conda info | awk '{print $4}' | sed -n '2p')"
}
  1. make the previous conda invisiable by adding changeps1: false in your ~/.condarc file.

Angryrou avatar Jul 30 '19 05:07 Angryrou

I edited @Angryrou's command slightly so that if there isn't an environment activated, it doesn't display anything:

# CONDA                                                                                                      
# Taken from https://github.com/caiogondim/bullet-train.zsh/issues/282#issuecomment-516266791
prompt_conda() {
  if [[ ! $(conda info | awk '{print $4}' | sed -n '2p') == 'None' ]]; then
    prompt_segment $BULLETTRAIN_VIRTUALENV_BG $BULLETTRAIN_VIRTUALENV_FG $BULLETTRAIN_VIRTUALENV_PREFIX" $(conda info | awk '{print $4}' | sed -n '2p')"
  fi
}

jrwrigh avatar Sep 08 '19 18:09 jrwrigh

Thanks @Angryrou and @jrwrigh for your solutions. I found the command "conda info" to be very slow on my machine so I wrote a different solution that uses the $CONDA_PREFIX variable. Hope it helps someone. NOTE: My solution removes $BULLETTRAIN_VIRTUALENV_PREFIX and sets $BULLETTRAIN_VIRTUALENV_FG to black just because I like it this way.

prompt_conda() {
        prefix=$(echo $CONDA_PREFIX | grep -o '[^/]*$')
        [[ -z $prefix ]] && return
        [[ $prefix = 'anaconda3' ]] && prefix='base'
        prompt_segment $BULLETTRAIN_VIRTUALENV_BG black $prefix
}

maxim-sm avatar May 12 '21 07:05 maxim-sm

Thanks to @Angryrou and @jrwrigh and @maxim-sm and @salmanulfarzy for your solutions. I found out that I can use CONDA_DEFAULT_ENV to get the currently active conda environment. This solution does not display anything if the base environment is activated or there's no active environment.

# Conda: current working env
prompt_conda() {
  if [[ -n $CONDA_DEFAULT_ENV && ! $CONDA_DEFAULT_ENV == 'base' ]]; then
    prompt_segment $BULLETTRAIN_VIRTUALENV_BG $BULLETTRAIN_VIRTUALENV_FG $BULLETTRAIN_VIRTUALENV_PREFIX" $CONDA_DEFAULT_ENV"
  fi
}

Jinsung-L avatar Jan 17 '23 01:01 Jinsung-L