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

本文详细介绍了Linux中环境变量PATH的设置,特别是Anaconda安装后对默认Python版本的影响。通过查看和编辑.bashrc文件,演示了如何调整环境变量顺序,使得系统Python优先于Anaconda Python执行,从而改变版本显示。最终通过添加PATH到.bashrc末尾并刷新,成功实现了版本切换。
摘要由CSDN通过智能技术生成

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

修改成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值