解决因为安装Anaconda引起的本机python版本改变的问题

linux会根据用户目录下的.bashrc文件内容来设置该用户的环境变量内容,当安装Anaconda之后该文件被添加Anaconda环境变量的设置内容,于是改变了系统原有的PATH的执行顺序,因此用户在python --verison的时候看到的便是Anaconda的默认putython版本

验证过程

可以通过echo $PATH来查看当前的环境变量设置顺序

[root@southcat southcat]# echo $PATH
/home/southcat/anaconda3/bin:/home/southcat/.local/bin/:/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/cuda/bin:/opt/cuda/nsight_compute:/opt/cuda/nsight_systems/bin:/home/southcat/.dotnet/tools:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

输入后可以看出 目前Anaconda的优先级是高于系统自带的

此时.bashrc文件内容如下

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

alias ls='ls --color=auto'
PS1='[\u@\h \W]\$ '
export PATH=/home/southcat/.local/bin/:$PATH

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

接下来按照传统的思路添加/usr/bin 到文件内,此时文件内容如下

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

alias ls='ls --color=auto'
PS1='[\u@\h \W]\$ '
export PATH=/home/southcat/.local/bin/:$PATH
export PATH=/usr/bin/:$PATH

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

接下来刷新一下在此查询版本

[southcat@southcat ~]$ python --version
Python 3.7.0

发现并未生效,重新查看$PATH

[southcat@southcat ~]$ echo $PATH
/home/southcat/anaconda3/bin:/usr/bin/:/home/southcat/.local/bin/:/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/cuda/bin:/opt/cuda/nsight_compute:/opt/cuda/nsight_systems/bin:/home/southcat/.dotnet/tools:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

发现Anaconda的优先级还是在第一位但是/usr/bin的优先级上来了,回去文件内容,发现该文件读取后的设置顺序是从后向前进行设置的,于是将export PATH=/usr/bin/:$PATH 添加到文件末尾source ~./bashrc刷新

[southcat@southcat ~]$ python --version
Python 3.9.6
[southcat@southcat ~]$ echo $PATH
/usr/bin/:/home/southcat/anaconda3/bin:/home/southcat/.local/bin/:/usr/local/sbin:/usr/local/bin:/usr/bin:/opt/cuda/bin:/opt/cuda/nsight_compute:/opt/cuda/nsight_systems/bin:/home/southcat/.dotnet/tools:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

修改成功

原创文章,作者:南猫,如若转载,请注明出处:https://southcat.net/2820.html


不妨点个广告再走嘛

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注