Python版本切换

一般服务器上可能会装有多个python版本,为了方便切换版本,下面记录一下怎么操作实现

1.查看已安装的python版本:

ls -l /usr/bin/python*
lrwxrwxrwx 1 root root       7 Mar  4  2019 /usr/bin/python -> python2
lrwxrwxrwx 1 root root      14 Mar  4  2019 /usr/bin/python-config -> python2-config
lrwxrwxrwx 1 root root       9 Mar  4  2019 /usr/bin/python2 -> python2.7
lrwxrwxrwx 1 root root      16 Mar  4  2019 /usr/bin/python2-config -> python2.7-config
-rwxr-xr-x 1 root root 3689352 Oct 10 22:02 /usr/bin/python2.7
lrwxrwxrwx 1 root root      33 Oct 10 22:02 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx 1 root root       9 Mar 26  2019 /usr/bin/python3 -> python3.7
-rwxr-xr-x 2 root root 4877888 Apr  3  2019 /usr/bin/python3.7
-rwxr-xr-x 2 root root 4877888 Apr  3  2019 /usr/bin/python3.7m
lrwxrwxrwx 1 root root      10 Mar 26  2019 /usr/bin/python3m -> python3.7m

2.用update-alternatives来管理切换版本

update-alternatives --list python

如果出现 update-alternatives: error:no alternatives for python,则说明Python版本未被update-alternatives识别,需要先添加一下:
update-alternatives: --install needs <link> <name> <path> <priority>

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

现在可以查看到了:

update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.7

3.切换版本命令

输入数字即可切换

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:

评论

暂无

添加新评论