问题与背景
安装好的ubuntu系统,默认是自带python2.7与python3.x的。但是python3.x有时候不是我们的开发版本,这时候就需要用到”升级“。本文用的策略也不是传统意义上的卸载重装,是安装之后,将python3指令指向新安装的内容。
参考资料
https://www.jb51.net/article/182392.htm 将 Ubuntu 16 和 18 上的 python 升级到最新 python3.8 的方法教程
https://blog.csdn.net/JasonDing1354/article/details/50470109 【Linux】使用update-alternatives命令进行版本的切换
案例与解决方案
套路就是通过更改指令的指向,然后安装多个python版本,去灵活切换一下python指令执行的版本。
1.升级一下ubuntu的软件库
$ sudo apt update
$ sudo apt install software-properties-common
2.添加deadsnakes PPA 源,因为这里有python的高版本,有时候默认镜像没有高版本的python
sudo add-apt-repository ppa:deadsnakes/ppa
3.安装python3.7
sudo apt install python3.7
4.将python版本添加到python3的可选执行路径
$ which python3.8
/usr/bin/python3.8
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
$ which python3.5
/usr/bin/python3.5
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2
5.配置python3指向,默认的路径。
$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.5 2 auto mode
1 /usr/bin/python3.5 2 manual mode
2 /usr/bin/python3.8 1 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
6.测试 python 版本
python3 -V