一、查看python版本

python --verstion

二、查看python版本指向

ls -l /usr/bin | grep python

此命令信息较详细,也可使用简洁命令:ls /usr/bin/python*

三、直接升级

从版本2升级到3,可使用命令:apt-get install python3.7。若报错,只能从源码安装。

若报此错E: Couldn't find any package by regex 'python3.7-dev'则

sudo add-apt-repository ppa:deadsnakes/ppa

sudo apt-get update

sudo apt-get install python3.7

sudo add-apt-repository ppa:jonathonf/python-3.7

sudo apt-get update

sudo apt-get install python3.7

若上述运行sudo add-apt-repository ppa:deadsnakes/ppa 出现 “bash: add-apt-repository: command not found” 则先运行:

sudo apt-get install -y software-properties-common

四、切换python版本

update-alternatives --list python#查看可替代python版本,如果报错没有可替代版本,则需要更新替代列表(操作如下)

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1

update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

若报错:update-alternatives: error: no alternatives for python,说明python 的替代版本尚未被 update-alternatives 命令识别,要更新一下替代列表,将 python2.7 和 python3.*放入其中。

# 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

# update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2

update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

也可移除代替版本:

# update-alternatives --remove python /usr/bin/python2.7

update-alternatives: removing manually selected alternative - switching python to auto mode

update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode

五、进行切换

update-alternatives --config python#选择标号