這個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