如何在.bashrc中将别名功能与conda初始化结合 [英] How to combine a function of alias with conda initialize in .bashrc

查看:391
本文介绍了如何在.bashrc中将别名功能与conda初始化结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在.bashrc
中合并两个不同的部分,我的目的是在尝试初始化conda之前将PYTHONPATH初始化为空。

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

在.bashrc中,

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 <<<

但是,我无法使用默认的PYTHONPATH,因为除非我想使用conda。因此,我创建了conda()别名。但这与使用conda初始化部件产生冲突。

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.

错误消息如下所示。

CommandNotFoundError:您的外壳未正确配置为使用'conda activate'。

这意味着未执行conda初始化

This means conda initialize was not executed.

这是conda()。

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

.basrh的行为应类似于我尝试在命令行上使用conda语句时清除PYTHONPATH的行为仅在conda初始化工作之前第一次。如果我不尝试使用conda语句,则PYTHONPATH应该是不可触摸的。

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.

推荐答案

我不确定我是否遵循所发生的事情或您要尝试执行的操作,但这是您可以尝试的方法:

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

然后,您可以使用 conda 函数。

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.

请注意,一旦您的 conda 函数运行后,即使在状态 PYTHONPATH 仍将为空。 > false 除非另有设置将其设置为非null值。因此,我不太确定该条件的意图。

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.

这篇关于如何在.bashrc中将别名功能与conda初始化结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆