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
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
003、调用python2测试
root@ubuntu01:/home# python2 Command 'python2' not found, but can be installed with: apt install python2
004、查看是否配置了Python替代方案
root@ubuntu01:/home# update-alternatives --list python update-alternatives: error: no alternatives for python
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
007、分别调用python2和python3 测试
root@ubuntu01:/home# python2 --version ## python2 Python 2.7.18 root@ubuntu01:/home# python3 --version ## python3 Python 3.10.6
008、查看是否配置了python替代方案
root@ubuntu01:/home# update-alternatives --list python update-alternatives: error: no alternatives for python
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
010、查看设置的python替代方案是否成功
root@ubuntu01:/home# update-alternatives --list python ## 产看python替代方案 /usr/bin/python2 /usr/bin/python3
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
012、测试切换效果
root@ubuntu01:/home# python --version
参考:https://blog.csdn.net/ai1648410251/article/details/121224177