首页 > 解决方案 > Python:使用 anacoda3 安装 python 3 后将 python2 设置为默认值

问题描述

我使用 anaconda3 进行 python3 安装。现在它是我的默认 python:

$ which python
/home/xy/anaconda3/bin/python
$ which python3
/home/xy/anaconda3/bin/python

但我需要 python2 作为我的默认 python。

$ which python2
/usr/bin/python2

我试图编辑我的 .bashrc 如下所示,

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

通过将 export PATH... 的行更改为

export PATH="$PATH:/home/xy/anaconda3/bin"

它没有改变任何东西。

我应该如何将 python2 设置为默认值?

标签: pythonanacondaanaconda3

解决方案


我认为最干净的方法是进行以下更改:

1)编辑您~/.bashrc并进行以下修改

保留此块。不要编辑它。如果您已经删除它,您可以通过键入重新创建它conda init bash

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

2)确保您/home/xy/anaconda3/bin没有添加到此块之外的 PATH 中。如果是这样,请删除那些。

3)调用conda config --set auto_activate_base False你的shell

从现在开始,你必须手动激活 anaconda 环境conda activate base。如果你不调用这个,你将默认回到你的系统 python。


推荐阅读