在linux下修改环境变量,切换不同版本的python的path
系统变量path修改了怎么恢复这个Linux系统下本来装了python2,⽽且是好⼏个。还装了anaconda,⾃带python3。我的⽬的是想让python环境变量默认使⽤python3。
查看当前python版本:
[liusiyi@localhost ~]$ python --version
Python 2.7.5
⽤which 看⼀下当前使⽤的python的路径:
[liusiyi@localhost ~]$ which python
/usr/bin/python
⽤whereis 确认所有python路径(但这个不全,因为没有anaconda):
[liusiyi@localhost ~]$ whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/bin/python2.7-config /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/
改⽤echo $PATH再次确认(这⾥也没包含anaconda):
[liusiyi@localhost ~]$ echo $PATH
/appcom/kylin/bin:/appcom/hadoop/bin:/appcom/hadoop/sbin:/usr/lib/jdk/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/shijuan355/bin:/appcom/hadoop/bin:/appcom/hadoop/sbin:/home/shijuan355/bin:/appcom/ganglia/sbin:/appcom/gang
⽤export PATH=添加python3路径:
[liusiyi@localhost ~]$ export PATH=/appcom/AnacondaInstall/anaconda3/bin:$PATH
这时候⽤python --version 看还是Python 2.7.5。因为bash命令export PATH=/appcom/AnacondaInstall/anaconda3/bin:$PATH,使PATH⾃增,既
PATH=PATH+"/appcom/AnacondaInstall/anaconda3/bin:";通常是把这⾏bash命令写到末尾。
下⾯采⽤在~/.bashrc ⾥添加⼀⾏“export PATH=/appcom/AnacondaInstall/anaconda3/bin:$PATH”的⽅式,这个只对当前⽤户⽣效,是⽐较稳妥的做法。
修改前的 ~/.bashrc:
[liusiyi@localhost  ~]$ cat ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
修改后的 ~/.bashrc,最后多了⼀⾏:
[liusiyi@localhost  ~]$ cat ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
export PATH=/appcom/AnacondaInstall/anaconda3/bin:$PATH
这时候有两个⽅法让这个环境变量⽣效。1、关闭当前终端窗⼝,重新打开⼀个新终端窗⼝就能⽣效;2、输⼊“source ~/.bashrc”命令,⽴即⽣效。
现在再来查看,python3已经成为默认的环境变量:
[liusiyi@localhost ~]$ which python
/appcom/AnacondaInstall/anaconda3/bin/python
[liusiyi@localhost ~]$ python --version
Python 3.6.3 :: Anaconda, Inc.
还有很多其他⽅法能实现不同版本的python切换环境变量默认值。很多⽂章都讲过。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。