Ubuntu18.04默认安装了python2.7。
注意,系统自带的python最好不要卸载!
输入命令python --version,可见默认安装的为python2.7
ubuntu@VM-186-195-ubuntu:~$ python --version
Python 2.7.15rc1
接下来安装python3.6
输入命令: sudo add-apt-repository ppa:jonathonf/python-3.6
中间按下Enter键确认
ubuntu@VM-186-195-ubuntu:~$ sudo add-apt-repository ppa:jonathonf/python-3.6
A plain backport of *just* Python 3.6. System extensions/Python libraries may or may not work.
Don't remove Python 3.5 from your system - it will break.
More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6
Press [ENTER] to continue or Ctrl-c to cancel adding it.
Hit:1 http://mirrors.tencentyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu bionic-updates InRelease
Get:4 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu bionic InRelease [15.9 kB]
Get:5 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu bionic/main i386 Packages [4,936 B]
Get:6 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu bionic/main amd64 Packages [4,928 B]
Get:7 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu bionic/main Translation-en [2,148 B]
Fetched 27.9 kB in 3s (8,822 B/s)
Reading package lists... Done
接下来输入命令:sudo apt-get update
ubuntu@VM-186-195-ubuntu:~$ sudo apt-get update
Hit:1 http://mirrors.tencentyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu bionic-security InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu bionic-updates InRelease
Hit:4 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu bionic InRelease
Reading package lists... Done
输入命令:sudo apt-get install python3.6 等待运行完成
ubuntu@VM-186-195-ubuntu:~$ sudo apt-get install python3.6
Reading package lists... Done
Building dependency tree
Reading state information... Done
........
调整Python3的优先级,使得3.6优先级较高
命令:sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
ubuntu@VM-186-195-ubuntu:~$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
更改默认值,python默认为Python2,现在修改为Python3
命令:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
ubuntu@VM-186-195-ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
ubuntu@VM-186-195-ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
此处结束,再次输入命令python --version
ubuntu@VM-186-195-ubuntu:~$ python --version
Python 3.6.8
可以看到此时的python版本已经是3.6了。
参考链接:参考链接