这个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/python.1.gz
改用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/ganglia/bin:/appcom/sqoop/bin:/appcom/hive/bin:/appcom/hbase/bin:/appcom/zookeeper/bin:/appcom/R/bin:/appcom/apache/bin:/appcom/oozie/bin:/appcom/btrace/bin:/appcom/storm/bin:/appcom/rocketmq//bin:/appcom/hbase/bin:/appcom/hbase/sbin:/appcom/R2/bin:/appcom/R3/bin:/appcom/spark/bin:/appcom/spark/sbin:/appcom/kafka/bin:/appcom/flume/bin:/appcom/odpp/bin:
用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切换环境变量默认值。很多文章都讲过。
python中path路径的优先匹配顺序:https://blog.csdn.net/wangzhaotongalex/article/details/50127431
切换Python2和Python3的4种方法:https://blog.csdn.net/jasonfqw/article/details/72974187