這個Linux系統下原本裝了python2,並且是好幾個。還裝了anaconda,自帶python3。個人目的是想讓python環境變量默認使用python3。python
查看當前python版本:apache
[liusiyi@localhost ~]$ python --version Python 2.7.5
用which 看一下當前使用的python的路徑:bash
[liusiyi@localhost ~]$ which python /usr/bin/python
用whereis 確認全部python路徑(但這個不全,由於沒有anaconda):app
[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):python2.7
[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路徑:oop
[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命令寫到末尾。spa
下面採用在~/.bashrc 裏添加一行「export PATH=/appcom/AnacondaInstall/anaconda3/bin:$PATH」的方式,這個只對當前用戶生效,是比較穩妥的作法。.net
修改前的 ~/.bashrc:code
[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,最後多了一行:orm
[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
這時候有兩個方法讓這個環境變量生效。一、關閉當前終端窗口,從新打開一個新終端窗口就能生效;二、輸入「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