How to combine a function of alias with conda initialize in .bashrc

狂风中的少年 提交于 2019-12-13 03:46:56

问题


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:


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.



来源:https://stackoverflow.com/questions/57542401/how-to-combine-a-function-of-alias-with-conda-initialize-in-bashrc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!