How to combine a function of alias with conda init

2019-08-24 06:17发布

I need to combine two different parts in .bashrc My purpose is to initialize PYTHONPATH as empty before try to initialize conda.

In .bashrc, there is conda initialize part.

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/me/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
  eval "$__conda_setup"
else
  if [ -f "/home/me/anaconda3/etc/profile.d/conda.sh" ]; then
    . "/home/me/anaconda3/etc/profile.d/conda.sh"
  else
    export PATH="/home/me/anaconda3/bin:$PATH"
  fi
fi
unset __conda_setup
# <<< conda initialize <<<

But, I have trouble with default PYTHONPATH because I couldn't touch this default except when I want to use conda. So, I create conda() alias. But it comes into conflict with using conda initialize part.

An error message is showed like below.

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

This means conda initialize was not executed.

Here is conda().

state=true
conda() {
  if $state; then
    state=false
    PYTHONPATH=''
    command conda "$@"
  else
    command conda "$@"
  fi
}

The .basrh should behavior like once I try to use conda statement on the command line, clearing PYTHONPATH only for the first time before conda initialize works. PYTHONPATH should be untouchable if I'm not trying to use conda statement.

1条回答
何必那么认真
2楼-- · 2019-08-24 07:01

I'm not sure I follow what's going on or what you're trying to do, but this is something for you to try:

savePYTHONPATH=PYTHONPATH
PYTHONPATH=''

# the conda initialize section should go here

PYTHONPATH=$savePYTHONPATH

Then you can use your conda function.

If this doesn't work, then it still should provide you with some ideas on how to proceed.

Note that once your conda function is run, PYTHONPATH will stay null even after state is false unless something else sets it to a non-null value. So I'm not really sure what the conditional is intended to do.

查看更多
登录 后发表回答