ubuntu 22.04中安装python2


001、系统

root@ubuntu01:/home# hostnamectl
 Static hostname: ubuntu01
       Icon name: computer-vm
         Chassis: vm
      Machine ID: e6fb52b4c8414f29b06dc95d57b5846c
         Boot ID: 4ffb5c0e628241fe84425ca626565e00
  Virtualization: vmware
Operating System: Ubuntu 22.04.1 LTS
          Kernel: Linux 5.15.0-47-generic
    Architecture: x86-64

ubuntu 22.04中安装python2

 

 

002、查看系统已安装的python版本

root@ubuntu01:/home# ls /usr/bin/python*          ## 默认安装了python3
/usr/bin/python3     /usr/bin/python3-futurize
/usr/bin/python3.10  /usr/bin/python3-pasteurize

ubuntu 22.04中安装python2

 

 

003、调用python2测试

root@ubuntu01:/home# python2
Command 'python2' not found, but can be installed with:
apt install python2

ubuntu 22.04中安装python2

 

 

004、查看是否配置了Python替代方案

root@ubuntu01:/home# update-alternatives --list python
update-alternatives: error: no alternatives for python

ubuntu 22.04中安装python2

 

 

005、安装python2

root@ubuntu01:/home# apt install python2

 

006、检查已安装的python版本

root@ubuntu01:/home# ls /usr/bin/python*           ## 查看已安装的python版本
/usr/bin/python2    /usr/bin/python3     /usr/bin/python3-futurize
/usr/bin/python2.7  /usr/bin/python3.10  /usr/bin/python3-pasteurize

ubuntu 22.04中安装python2

 

 

007、分别调用python2和python3 测试

root@ubuntu01:/home# python2 --version         ## python2
Python 2.7.18
root@ubuntu01:/home# python3 --version         ## python3
Python 3.10.6

ubuntu 22.04中安装python2

 

 

008、查看是否配置了python替代方案

root@ubuntu01:/home# update-alternatives --list python
update-alternatives: error: no alternatives for python

ubuntu 22.04中安装python2

 

 

009、设置python替代方案

root@ubuntu01:/home# update-alternatives --install /usr/bin/python python /usr/bin/python2 1  ## 将python2设置为1
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
root@ubuntu01:/home# update-alternatives --install /usr/bin/python python /usr/bin/python3 2  ## 将python3设置为2
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

ubuntu 22.04中安装python2

 

 

010、查看设置的python替代方案是否成功

root@ubuntu01:/home# update-alternatives --list python    ## 产看python替代方案
/usr/bin/python2
/usr/bin/python3

ubuntu 22.04中安装python2

 

 

011、如何切换版本

root@ubuntu01:/home# update-alternatives --config python         ## 如何切换版本
There are 2 choices for the alternative python (providing /usr/bin/python).

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

Press <enter> to keep the current choice[*], or type selection number: 1      ## 此处选择1 python2
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in manual mode

ubuntu 22.04中安装python2

 

 

012、测试切换效果

root@ubuntu01:/home# python --version

ubuntu 22.04中安装python2

 

 

参考:https://blog.csdn.net/ai1648410251/article/details/121224177

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289625.html

(0)
上一篇 2022年9月15日
下一篇 2022年9月15日

相关推荐

发表回复

登录后才能评论