Ubuntu上python版本查看

$ python -V
Python 2.7.12

1.安装3.7

添加源

sudo add-apt-repository ppa:deadsnakes/ppa

检查更新

sudo apt-get update 

安装python3.7

sudo apt-get install python3.7

安装pip3

sudo apt install python3-pip

2.使用 update-alternatives 来为整个系统更改Python 版本

查看python替代版本信息

~$ update-alternatives --display python

但是结果为

update-alternatives: error: no alternatives for python

python的替代版本尚未被update-alternatives

查看python的位置

$ whereis python
python: /usr/bin/python2.7 /usr/bin/python3.7 /usr/bin/python3.5m /usr/bin/python3.7m /usr/bin/python3.5 /usr/bin/python /usr/lib/python2.7 /usr/lib/python3.7 /usr/lib/python3.5 /etc/python2.7 /etc/python3.7 /etc/python3.5 /etc/python /usr/local/lib/python2.7 /usr/local/lib/python3.7 /usr/local/lib/python3.5 /usr/share/python /usr/share/man/man1/python.1.gz

更新一下替代列表

$ 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
baby@ubuntu:~$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in auto mode

查看python替代版本信息

$ update-alternatives --display python                             python - auto mode
  link best version is /usr/bin/python3.7
  link currently points to /usr/bin/python3.7
  link python is /usr/bin/python
/usr/bin/python2.7 - priority 1
/usr/bin/python3.7 - priority 2

查看python版本

$ python -V
Python 3.7.9

3.切换版本

切换到2.7

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

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

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode
baby@ubuntu:~$ python -V
Python 2.7.12

pip版本

$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

切换到3.7

sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

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

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in manual mode
baby@ubuntu:~$ python -V
Python 3.7.9

安装pip

sudo apt install python3-pip

pip版本

 pip -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.7)

pip的版本随着python版本切换