Ubuntu16.04下切换Python版本

Ubuntu16.04下切换Python版本

查看你所有的python安装路径

~$ whereis python

python2切换到python3:

echo alias python=python3 >> ~/.bashrc
source ~/.bashrc
python3切换到python2:

gedit ~/.bashrc
# alias python=python3
source ~/.bashrc

查看:
python

对于ubuntu 16.04,由于本身是自带python,这样就减少了在windows下的下载和环境变量配置,非常不错。但是他本身是自带两个版本的python,2.X和3.X,两个版本,两个版本默认的是使用2.X,毕竟3.X是一个趋势,今后我这边应该也会主要使用3.X版本,所以这里记录一下如果在版本间切换以及如何把python版本切换到3.X下的方法。

注意:要以root身份操作

一: 确认本机下的python默认版本。调出终端,输入python即可查看默认的版本:

www.zeeklog.com  - Ubuntu16.04下切换Python版本

二:如果想要查看本机自带的python的另一个版本为多少,需要命令python3即可:

(博主电脑里自带的是2.7和3.5并没有3.6,我自己电脑的3.6是另外安装的,这个简单,就不多说了,但是输入Python3时显示的是3.5并不是3.6,显示3.6要输入Python3.6,估计是因为我电脑里面3.5的优先级高于3.6,关于优先级的操作,在下面)

www.zeeklog.com  - Ubuntu16.04下切换Python版本

三: 如何切换这两个版本以及切换默认的python版本:

我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录,首先罗列出所有可用的python 替代版本信息:

update-alternatives --list python
update-alternatives: error: no alternatives for python

如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7 和 python3.4 放入其中。

  1. # update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
  2. update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
  3. # update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
  4. update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

(这里我设置没有成功,但是我还是把电脑里的三个Python版本全都设置了一遍,最后还是成功切换Python版本了)

--install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先 级的选项就会被选中。这个例子中,我们为/usr/bin/python3.4 设置的优先级为2,所以update-alternatives 命 令会自动将它设置为默认 Python 版本。

  1. # python --version
  2. Python 3.5.2

(因为上面博主没有设置成功,所以这一步显示的还是2.7)

接下来,我们再次列出可用的 Python 替代版本。

  1. # update-alternatives --list python
  2. /usr/bin/python2.7
  3. /usr/bin/python3.5

现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。

  1. # update-alternatives --config python

接下来,我们再次列出可用的 Python 替代版本。

  1. # update-alternatives --list python
  2. /usr/bin/python2.7
  3. /usr/bin/python3.5

现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。

(这一步是最关键的)

  1. # update-alternatives --config python

下面就简单了,会提示你输入序号,你想用哪个版本为默认,就输入序号就可以了!

www.zeeklog.com  - Ubuntu16.04下切换Python版本

实测:

peter@peter-Joybook-S53:~$ sudo update-alternatives --list python
[sudo] password for peter:
update-alternatives: error: no alternatives for python

peter@peter-Joybook-S53:$ python --version
Python 2.7.12
peter@peter-Joybook-S53:$ python3 --version
Python 3.5.2
peter@peter-Joybook-S53:~$ update-alternatives --install /usr/bin/python python /usr/bin/python2.7.12
update-alternatives: --install needs

Use 'update-alternatives --help' for program usage information.
peter@peter-Joybook-S53:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7.12
update-alternatives: --install needs

Use 'update-alternatives --help' for program usage information.
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode
peter@peter-Joybook-S53:$ python --version
Python 3.5.2
peter@peter-Joybook-S53:$ sudo update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.5
peter@peter-Joybook-S53:~$ update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.5 2 auto mode
1 /usr/bin/python2.7 1 manual mode
2 /usr/bin/python3.5 2 manual mode

Press  to keep the current choice[*], or type selection number:
peter@peter-Joybook-S53:~$ python --version
Python 3.5.2

新安装了python3.7, 配置如下:

peter@peter-Joybook-S53:$export PATH=/Python-3.7.2:$PATH
peter@peter-Joybook-S53:$ echo $PATH
/home/peter/Python-3.7.2:/home/peter/bin:/home/peter/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
peter@peter-Joybook-S53:$ python -version
Unknown option: -e
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
peter@peter-Joybook-S53:$python --version
Python 3.7.2
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7.2 5
update-alternatives: error: alternative path /usr/bin/python3.7.2 doesn't exist
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python /home/peter/python3.7.2 5
update-alternatives: error: alternative path /home/peter/python3.7.2 doesn't exist
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python /home/peter/Python3.7.2 5
update-alternatives: error: alternative path /home/peter/Python3.7.2 doesn't exist
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python ~/Python3.7.2 5
update-alternatives: error: alternative path /home/peter/Python3.7.2 doesn't exist
peter@peter-Joybook-S53:$ sudo update-alternatives --install /usr/bin/python python /Python-3.7.2 5
update-alternatives: using /home/peter/Python-3.7.2 to provide /usr/bin/python (python) in auto mode
peter@peter-Joybook-S53:$ update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
------------------------------------------------------------
* 0 /home/peter/Python-3.7.2 5 auto mode
1 /home/peter/Python-3.7.2 5 manual mode
2 /home/peter/Python-3.7.2/python 3 manual mode
3 /usr/bin/python2.7 1 manual mode
4 /usr/bin/python3.5 4 manual mode

Press  to keep the current choice[*], or type selection number:
peter@peter-Joybook-S53:$ python --version
Python 3.7.2
peter@peter-Joybook-S53:$ python
Python 3.7.2 (default, Feb 2 2019, 18:25:26)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.