vlambda博客
学习文章列表

【Linux踩坑系列】:删除系统自带python2环境引发的问题

7.22日,已升级 VMw are W orkstation为16,并选择了稳定且长期支持的版本CentOS7.9系统。
之前用的Centos8.1系统是没有默认Python环境,所以,在之前的那篇文章 《 没有涉及到如何卸载自带的Python环境,而CentOS7.x默认是2.7.5版本,这里打算卸载并安装3.8.1版本,没想到这一删除操作引发了一系列的问题,踩了不少坑。 先来 看一下 卸载系统自带 P ython 2的过程:
  • 卸载之前,使用有关的查询命令看下:
[root@localhost opt]# python --versionPython 2.7.5[root@localhost opt]# which python/usr/bin/python[root@localhost opt]# whereis pythonpython: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz[root@localhost opt]# rpm -qa|grep pythonlibpeas-loader-python-1.22.0-1.el7.x86_64dbus-python-1.1.1-9.el7.x86_64python-augeas-0.5.0-2.el7.noarchspeech-dispatcher-python-0.7.1-15.el7.x86_64python-decorator-3.4.0-3.el7.noarchpython-pwquality-1.2.3-5.el7.x86_64libstoragemgmt-python-clibs-1.8.1-2.el7_9.x86_64python-linux-procfs-0.4.11-4.el7.noarchlibuser-python-0.60-9.el7.x86_64libsemanage-python-2.5-14.el7.x86_64python-gobject-base-3.22.0-1.el7_4.1.x86_64libreport-python-2.1.11-53.el7.centos.x86_64python-firewall-0.6.3-13.el7_9.noarchliblouis-python-2.5.2-12.el7_4.noarchlibselinux-python-2.5-15.el7.x86_64python-cups-1.9.63-6.el7.x86_64python-IPy-0.75-6.el7.noarchpython-ethtool-0.8-8.el7.x86_64python-libs-2.7.5-90.el7.x86_64audit-libs-python-2.8.5-4.el7.x86_64python-smbc-1.0.13-8.el7.x86_64python-inotify-0.9.4-4.el7.noarchpython-di-0.3-2.el7.noarchpolicycoreutils-python-2.5-34.el7.x86_64python-lxml-3.2.1-4.el7.x86_64python-ntplib-0.3.2-1.el7.noarchpython-meh-0.25.3-1.el7.noarchpython-pyblock-0.53-6.el7.x86_64langtable-python-0.0.31-4.el7.noarchpython-setuptools-0.9.8-7.el7.noarchpython-configobj-4.7.2-7.el7.noarchpython-chardet-2.2.1-3.el7.noarchsystemd-python-219-78.el7_9.3.x86_64python-urlgrabber-3.10-10.el7.noarchpython-meh-gui-0.25.3-1.el7.noarchlibxml2-python-2.9.1-6.el7.5.x86_64python-coverage-3.6-0.5.b3.el7.x86_64python-schedutils-0.4-6.el7.x86_64python-pyudev-0.15-9.el7.noarchpython-kitchen-1.1.1-5.el7.noarchpython-blivet-0.61.15.76-1.el7_9.noarchpython-deltarpm-3.6-3.el7.x86_64python-gobject-3.22.0-1.el7_4.1.x86_64python-slip-0.4.0-4.el7.noarchpython-ipaddress-1.0.16-2.el7.noarchpython2-futures-3.1.1-5.el7.noarchnewt-python-0.52.15-4.el7.x86_64cryptsetup-python-2.0.3-6.el7.x86_64python2-blockdev-2.18-5.el7.x86_64abrt-python-2.1.11-60.el7.centos.x86_64python-perf-3.10.0-1160.31.1.el7.x86_64python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarchpython-2.7.5-90.el7.x86_64python-brlapi-0.6.0-16.el7.x86_64python-backports-1.0-8.el7.x86_64python-nss-0.16.0-3.el7.x86_64rpm-python-4.11.3-45.el7.x86_64python-six-1.9.0-2.el7.noarchlibstoragemgmt-python-1.8.1-2.el7_9.noarchpython2-subprocess32-3.2.6-14.el7.x86_64python-iniparse-0.4-9.el7.noarchpython-pycurl-7.19.0-19.el7.x86_64abrt-addon-python-2.1.11-60.el7.centos.x86_64python2-pyatspi-2.26.0-3.el7.noarchpython-slip-dbus-0.4.0-4.el7.noarch

  • 强制卸载自带的python以及相关联的程序,并查看卸载后的情况:
[root@localhost opt]# rpm -qa | grep python | xargs rpm -e --allmatches --nodeps[root@localhost opt]# rpm -qa|grep python[root@localhost opt]# whereis pythonpython: /usr/lib/python2.7 /usr/lib64/python2.7[root@localhost opt]# which python/usr/bin/which: no python in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)[root@localhost opt]# python --versionbash: /usr/bin/python: 没有那个文件或目录

lib、lib64等未删除完,继续使用以下命令~

  • 删除所有与python相关的残余文件,并查看卸载后的情况:
[root@localhost opt]# whereis python | xargs rm -rf[root@localhost opt]# rpm -qa|grep python[root@localhost opt]# whereis python[root@localhost opt]# which python/usr/bin/which: no python in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)[root@localhost opt]# python --versionbash: /usr/bin/python: 没有那个文件或目录

终于删除干净了,我以为这样愉快的安装部署Python3了。可是,想法太天真了......,当执行 修改安装路径 命令时,报错找不到gcc,通过gcc -v发现没有安装gcc和make插件,于是执行了yum install gcc gcc-c++,报错如下:

[root@localhost Python-3.8.1]# yum install gcc gcc-c++bash: /usr/bin/yum: /usr/bin/python: 坏的解释器: 没有那个文件或目录

这是因为yum依赖python2。因此,删除python2肯定使用不了yum,给自己埋了一个大坑啊。好吧,自己造的孽自己解决吧,于是把yum也给卸载了,重新安装......

可以看出, 这是因为yum依赖python2,删除python2肯定使用不了yum,给自己埋了一个大坑啊。好吧,自己造的孽自己解决吧,于是要把yum也给卸载了,重新安装......
  • 卸载删除yum前查询一下:

[root@localhost opt]# whereis yumyum: /usr/bin/yum /etc/yum /etc/yum.conf /usr/share/man/man8/yum.8[root@localhost opt]# which yum/usr/bin/yum[root@localhost opt]# rpm -qa|grep yumyum-plugin-fastestmirror-1.1.31-54.el7_8.noarchyum-metadata-parser-1.1.4-10.el7.x86_64yum-3.4.3-168.el7.centos.noarchyum-utils-1.1.31-54.el7_8.noarchyum-langpacks-0.4.2-7.el7.noarchPackageKit-yum-1.1.10-2.el7.centos.x86_64[root@localhost opt]# yum -versionbash: /usr/bin/yum: /usr/bin/python: 坏的解释器: 没有那个文件或目录
  • 执行以下删除命令:

[root@localhost opt]# rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps[root@localhost opt]# rm -rf /etc/yum.repos.d/*
再查看下发现yum已被卸载移除,下面进行重新安装yum......
当然,需要知道Linux当前版本及位数:
[root@localhost /]# cat /etc/centos-releaseCentOS Linux release 7.9.2009 (Core)[root@localhost /]# getconf LONG_BIT64

    http://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/

lvm2-python-libs-2.02.187-6.el7.x86_64.rpmlibxml2-python-2.9.1-6.el7.5.x86_64.rpmyum-plugin-aliases-1.1.31-54.el7_8.noarch.rpmyum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpmyum-plugin-protectbase-1.1.31-54.el7_8.noarch.rpmyum-metadata-parser-1.1.4-10.el7.x86_64.rpmyum-3.4.3-168.el7.centos.noarch.rpmyum-utils-1.1.31-54.el7_8.noarch.rpmpython-chardet-2.2.1-3.el7.noarch.rpmpython-kitchen-1.1.1-5.el7.noarch.rpmpython-setuptools-0.9.8-7.el7.noarch.rpmpython-urlgrabber-3.10-10.el7.noarch.rpmpython-pycurl-7.19.0-19.el7.x86_64.rpmpython-iniparse-0.4-9.el7.noarch.rpmpython-2.7.5-89.el7.x86_64.rpmpython-backports-1.0-8.el7.x86_64.rpmpython-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpmpython-ipaddress-1.0.16-2.el7.noarch.rpmpython-libs-2.7.5-89.el7.x86_64.rpmrpm-python-4.11.3-45.el7.x86_64.rpm
使用wget命令,把这些rpm包下载到新建的目录/opt/rpm-bak/,之后执行安装命令:
rpm -Uvh --replacepkgs lvm2-python-libs*.rpm --nodeps --forcerpm -Uvh --replacepkgs libxml2-python*.rpm --nodeps --forcerpm -Uvh --replacepkgs python*.rpm --nodeps --forcerpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm --nodeps --force
重新查询下Python环境:
[root@localhost /]# python --versionPython 2.7.5[root@localhost rpm-bak]# which python/usr/bin/python[root@localhost rpm-bak]# whereis pythonpython: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz[root@localhost rpm-bak]# rpm -qa|grep pythonpython-setuptools-0.9.8-7.el7.noarchpython-backports-1.0-8.el7.x86_64python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarchpython-ipaddress-1.0.16-2.el7.noarchrpm-python-4.11.3-45.el7.x86_64libxml2-python-2.9.1-6.el7.5.x86_64python-urlgrabber-3.10-10.el7.noarchlvm2-python-libs-2.02.187-6.el7.x86_64python-pycurl-7.19.0-19.el7.x86_64python-2.7.5-89.el7.x86_64python-kitchen-1.1.1-5.el7.noarchpython-libs-2.7.5-89.el7.x86_64python-chardet-2.2.1-3.el7.noarchpython-iniparse-0.4-9.el7.noarch
运行下载gcc和make的yum命令:
[root@localhost /]# yum install gcc gcc-c++已加载插件:aliases, fastestmirror, protectbaseLoading mirror speeds from cached hostfileThere are no enabled repos. Run "yum repolist all" to see the repos you have. To enable Red Hat Subscription Management repositories: subscription-manager repos --enable <repo> To enable custom repositories:     yum-config-manager --enable <repo>
这里的意思是说,没有可用的镜像源repo,前往/etc/yum.repos.d/查看是否存在镜像源,发现是没有的......
[root@localhost /]# cd /etc/yum.repos.d/[root@localhost yum.repos.d]# ll总用量 0

那么我就使用阿里和网易的吧,设置过程如下:

# 下载阿里和网易源到/etc/yum.repos.d/wget http://mirrors.aliyun.com/repo/Centos-7.repowget http://mirrors.163.com/.help/CentOS7-Base-163.repo# 清除系统所有的yum缓存yum clean all# 生成yum缓存yum makecache# 安装epel源yum install -y epel-release# 使用阿里开源镜像提供的epel源wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo# 再次清除系统yum缓存,并重新生成新的yum缓存yum clean allyum makecache# 查看系统可用的yum源和所有的yum源yum repolist enabled
再次执行  yum install gcc gcc-c++  等安装命令,已经可以了~~

哎,yum依赖Python2而存在,而删除自带的Python2是有风险的。上面的一顿操作,当于恢复之前的删除系统自带Python2环境。踩的挺深这个以后要注意啦!

之后,那篇文章进行部署新的Python3环境,但发现 python --version 仍旧是2.7.5版本,这个问题该如何解决呢? 既然 不可删除 ,那就共存 吧, 就是让 Python 2 .7.5 Py thon 3.8.1 都可以使用 进入 /usr/bin / 使用 命令 ll python* 下设 置:
[root@localhost bin]# ll python*lrwxrwxrwx. 1 root root 7 7月 24 20:59 python -> python2lrwxrwxrwx. 1 root root 9 7月 24 20:59 python2 -> python2.7-rwxr-xr-x. 1 root root 7144 10月 14 2020 python2.7

这是什么意思呢,看下 Python 版本查询就知道了:

[root@localhost bin]# ^C[root@localhost bin]# python2 --versionPython 2.7.5[root@localhost bin]# python --versionPython 2.7.5

就是说,python指向的是python2,而python2又指向了python2.7,终端输入pythonpython2都可以用到python2.7.5的环境,这里我们把python的指向改为python3,就可以实现python2python3共存了。

之前那篇文章需要改动的是软连接创建目标路径,把 /usr/local/bin/python 改成 /usr/bin/python  即可。先把之前的软连接删除掉:

[root@localhost bin]# pwd/usr/local/bin[root@localhost bin]# ll python*lrwxrwxrwx. 1 root root 31 724 21:44 python -> /usr/local/python38/bin/python3[root@localhost bin]# rm -rf python [root@localhost bin]# ll python*ls: 无法访问python*: 没有那个文件或目录[root@localhost bin]# ll pip*lrwxrwxrwx. 1 root root 28 724 21:44 pip -> /usr/local/python38/bin/pip3[root@localhost bin]# rm -rf pip [root@localhost bin]# ll pip*ls: 无法访问pip*: 没有那个文件或目录
再重新创建软连接:
[root@localhost bin]# pwd/usr/bin# 先备份一下[root@localhost bin]# cp python python-bak[root@localhost bin]# ln -s /usr/local/python38/bin/python3 /usr/bin/pythonln: 无法创建符号链接"/usr/bin/python": 文件已存在[root@localhost bin]# ll python*lrwxrwxrwx. 1 root root 7 7月 24 20:59 python -> python2lrwxrwxrwx. 1 root root 9 7月 24 20:59 python2 -> python2.7-rwxr-xr-x. 1 root root 7144 10月 14 2020 python2.7-rwxr-xr-x. 1 root root 7144 7月 24 22:18 python-bak[root@localhost bin]# rm -rf python[root@localhost bin]# ll python*lrwxrwxrwx. 1 root root 9 7月 24 20:59 python2 -> python2.7-rwxr-xr-x. 1 root root 7144 10月 14 2020 python2.7-rwxr-xr-x. 1 root root 7144 7月 24 22:18 python-bak[root@localhost bin]# ln -s /usr/local/python38/bin/python3 /usr/bin/python[root@localhost bin]# ll python*lrwxrwxrwx. 1 root root 31 7月 24 22:27 python -> /usr/local/python38/bin/python3lrwxrwxrwx. 1 root root 9 7月 24 20:59 python2 -> python2.7-rwxr-xr-x. 1 root root 7144 10月 14 2020 python2.7-rwxr-xr-x. 1 root root 7144 7月  24 22:18 python-bak
python环境设置的效果:
[root@localhost /]# python --versionPython 3.8.1[root@localhost /]# python2 --versionPython 2.7.5

而有时候撸Python代码需要使用pip下载第三方库,pip3的设置:

[root@localhost bin]# pwd/usr/bin[root@localhost bin]# ll pip*ls: 无法访问pip*: 没有那个文件或目录[root@localhost bin]# ln -s /usr/local/python38/bin/pip3 /usr/bin/pip3[root@localhost bin]# ll pip*lrwxrwxrwx. 1 root root 28 724 22:51 pip3 -> /usr/local/python38/bin/pip3[root@localhost bin]# pip3 listPackage Version---------- -------pip 21.1.3setuptools 41.2.0

这里只要为Python3设置pip3就行了,Python2不再设置对应的pip。

下载第三方库,通过 pip3 install 名称  即可:

总结一下:
经过这一番踩坑,整体上清楚了这么一个问题: yum 依赖于 python2 环境 ,python2 又是当前 CentOS7.9 的默认环境,我们不能像卸载 JDK 那样去卸载python2,否则 yum 则无法正常使用!!!另外,通过改变/usr/bin/下的python指向(默认指向python2),就可以部署新的Python3环境了。