解决ubuntu18.04 python2和python3冲突的问题

    技术2025-01-16  13

    版本须知

    ubuntu18.04 安装3个python版本,系统已自带python3.6.9和2.7.15,目前的旧链接指向python2,并且2和3产生了冲突,此时我们设置python3.6.9为默认即可

    注意事项

    切记不可随意删除系统自带python版本,因为系统有很多软件依赖着

    删除旧版本python的软链接指向

    1.查看python现有信息 root@chenwx:/usr/bin# ls /usr/bin/python* -lh lrwxrwxrwx 1 root root 24 Jun 9 14:45 /usr/bin/python -> /etc/alternatives/python lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python2 -> python2.7 -rwxr-xr-x 1 root root 3.5M Apr 16 01:20 /usr/bin/python2.7 lrwxrwxrwx 1 root root 9 Oct 25 2018 /usr/bin/python3 -> python3.6 -rwxr-xr-x 2 root root 4.4M Apr 18 09:56 /usr/bin/python3.6 lrwxrwxrwx 1 root root 34 Jun 9 14:41 /usr/bin/python368 -> /usr/local/python3.6.9/bin/python3 -rwxr-xr-x 2 root root 4.4M Apr 18 09:56 /usr/bin/python3.6m -rwxr-xr-x 1 root root 1018 Oct 29 2017 /usr/bin/python3-jsondiff -rwxr-xr-x 1 root root 3.6K Oct 29 2017 /usr/bin/python3-jsonpatch -rwxr-xr-x 1 root root 1.4K May 2 2016 /usr/bin/python3-jsonpointer -rwxr-xr-x 1 root root 398 Nov 16 2017 /usr/bin/python3-jsonschema lrwxrwxrwx 1 root root 10 Oct 25 2018 /usr/bin/python3m -> python3.6m 2.重命名旧的python软链 mv /usr/bin/python /usr/bin/python.bak mv /usr/bin/pip /usr/bin/pip.bak 3.建立python新软链和pip软链 ln -s /usr/local/python369/bin/python3 /usr/bin/python ln -s /usr/local/python369/bin/pip3 /usr/bin/pip 4.测试版本 root@chenwx:/usr/bin# python Python 3.6.9 (default, Jun 9 2020, 14:37:48) [GCC 7.5.0] on linux Type "help", "copyright", "credits" or "license" for more information.

    更新整个版本的python信息

    我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录(冒险,建议用普通用户外加sudo权限),首先罗列出所有可用的python 替代版本信息: #update-alternatives --list python update-alternatives: error: no alternatives for python 如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7 和 python3.6放入其中。 root@chenwx:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python2 100 update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode root@chenwx:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python3 110 update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode root@chenwx:/usr/bin# update-alternatives --install /usr/bin/python python /usr/local/python3.6.9/bin/python3 150 update-alternatives: using /usr/local/python3.6.9/bin/python3 to provide /usr/bin/python (python) in auto mode -install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中,操作上直接回车即可 root@chenwx:/usr/bin# sudo update-alternatives --config python There are 3 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3 110 auto mode 1 /usr/bin/python2 100 manual mode 2 /usr/bin/python3 110 manual mode

    参考文献

    参考文献1:设置python的文件依赖

    参考文献2:ros环境依赖于python2.7环境

    Processed: 0.009, SQL: 9