利用.bashrc来动态切换conda2和conda3

需求

最近想要从conda2切换到conda3,但是因为之前的许多应用还是依赖于python2的环境,于是就产生了动态切换conda3的需求

分析

笔者使用的是Ubuntu的环境,并且conda是安装在用户目录下的,也就是/home/user/anaconda2,成功安装conda3之后,也会在用户目录下生成目录/home/user/anaconda3,于是我想应该只需要动态切换一些环境变量就能实现动态切换了

解决方案

安装anaconda3

官方目录下载最新版本的anaconda3,笔者下载好的文件是:Anaconda3-2019.10-Linux-x86_64.sh, 在终端目录下运行:

$ bash Anaconda3-2019.10-Linux-x86_64.sh

在安装的最后一步会提示需不需要运行conda init,这个时候选择yes

image

这项功能会在~/.bashrc文件下生成一段shell代码

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

笔者之前安装的anaconda2生成的代码是这样的

# added by Anaconda2 5.3.0 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/user/anaconda2/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/home/user/anaconda2/etc/profile.d/conda.sh" ]; then
# . "/home/user/anaconda2/etc/profile.d/conda.sh"  # commented out by conda initialize
        CONDA_CHANGEPS1=false conda activate base
    else
        \export PATH="/home/user/anaconda2/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

于是笔者就想能不能把这2段初始化代码放到shell函数中,然后用一个参数选择初始化那一段代码

select_conda() {

    if [ $1 == "conda2" ]
    then
        # added by Anaconda2 5.3.0 installer
        # >>> conda init >>>
        # !! Contents within this block are managed by 'conda init' !!
        __conda_setup="$(CONDA_REPORT_ERRORS=false '/home/user/anaconda2/bin/conda' shell.bash hook 2> /dev/null)"
        if [ $? -eq 0 ]; then
            \eval "$__conda_setup"
        else
            if [ -f "/home/user/anaconda2/etc/profile.d/conda.sh" ]; then
        # . "/home/user/anaconda2/etc/profile.d/conda.sh"  # commented out by conda initialize
                CONDA_CHANGEPS1=false conda activate base
            else
                \export PATH="/home/user/anaconda2/bin:$PATH"
            fi
        fi
        unset __conda_setup
        # <<< conda init <<<
    fi

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

}

测试

当我想要选择anaconda2的时候,就在~/.bashrc中加上

select_conda "conda2"

重新打开一个终端:

$ python
Python 2.7.12 (default, Oct  8 2019, 14:14:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.

当我想要选择anaconda3的时候,就在~/.bashrc中加上

select_conda "conda3"

终端运行:

$ . ~/.bashrc
$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
posted @ 2019-11-26 21:23  浮生问道  阅读(1846)  评论(0编辑  收藏  举报