Linux踩坑系列:删除系统自带python2环境引发的问题
Posted 追风筝的路上
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux踩坑系列:删除系统自带python2环境引发的问题相关的知识,希望对你有一定的参考价值。
-
卸载之前,使用有关的查询命令看下:
[ ]
Python 2.7.5
[ ]
/usr/bin/python
[ ]
python: /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
[ ]
libpeas-loader-python-1.22.0-1.el7.x86_64
dbus-python-1.1.1-9.el7.x86_64
python-augeas-0.5.0-2.el7.noarch
speech-dispatcher-python-0.7.1-15.el7.x86_64
python-decorator-3.4.0-3.el7.noarch
python-pwquality-1.2.3-5.el7.x86_64
libstoragemgmt-python-clibs-1.8.1-2.el7_9.x86_64
python-linux-procfs-0.4.11-4.el7.noarch
libuser-python-0.60-9.el7.x86_64
libsemanage-python-2.5-14.el7.x86_64
python-gobject-base-3.22.0-1.el7_4.1.x86_64
libreport-python-2.1.11-53.el7.centos.x86_64
python-firewall-0.6.3-13.el7_9.noarch
liblouis-python-2.5.2-12.el7_4.noarch
libselinux-python-2.5-15.el7.x86_64
python-cups-1.9.63-6.el7.x86_64
python-IPy-0.75-6.el7.noarch
python-ethtool-0.8-8.el7.x86_64
python-libs-2.7.5-90.el7.x86_64
audit-libs-python-2.8.5-4.el7.x86_64
python-smbc-1.0.13-8.el7.x86_64
python-inotify-0.9.4-4.el7.noarch
python-di-0.3-2.el7.noarch
policycoreutils-python-2.5-34.el7.x86_64
python-lxml-3.2.1-4.el7.x86_64
python-ntplib-0.3.2-1.el7.noarch
python-meh-0.25.3-1.el7.noarch
python-pyblock-0.53-6.el7.x86_64
langtable-python-0.0.31-4.el7.noarch
python-setuptools-0.9.8-7.el7.noarch
python-configobj-4.7.2-7.el7.noarch
python-chardet-2.2.1-3.el7.noarch
systemd-python-219-78.el7_9.3.x86_64
python-urlgrabber-3.10-10.el7.noarch
python-meh-gui-0.25.3-1.el7.noarch
libxml2-python-2.9.1-6.el7.5.x86_64
python-coverage-3.6-0.5.b3.el7.x86_64
python-schedutils-0.4-6.el7.x86_64
python-pyudev-0.15-9.el7.noarch
python-kitchen-1.1.1-5.el7.noarch
python-blivet-0.61.15.76-1.el7_9.noarch
python-deltarpm-3.6-3.el7.x86_64
python-gobject-3.22.0-1.el7_4.1.x86_64
python-slip-0.4.0-4.el7.noarch
python-ipaddress-1.0.16-2.el7.noarch
python2-futures-3.1.1-5.el7.noarch
newt-python-0.52.15-4.el7.x86_64
cryptsetup-python-2.0.3-6.el7.x86_64
python2-blockdev-2.18-5.el7.x86_64
abrt-python-2.1.11-60.el7.centos.x86_64
python-perf-3.10.0-1160.31.1.el7.x86_64
python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch
python-2.7.5-90.el7.x86_64
python-brlapi-0.6.0-16.el7.x86_64
python-backports-1.0-8.el7.x86_64
python-nss-0.16.0-3.el7.x86_64
rpm-python-4.11.3-45.el7.x86_64
python-six-1.9.0-2.el7.noarch
libstoragemgmt-python-1.8.1-2.el7_9.noarch
python2-subprocess32-3.2.6-14.el7.x86_64
python-iniparse-0.4-9.el7.noarch
python-pycurl-7.19.0-19.el7.x86_64
abrt-addon-python-2.1.11-60.el7.centos.x86_64
python2-pyatspi-2.26.0-3.el7.noarch
python-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 python
python: /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 --version
bash: /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 --version
bash: /usr/bin/python: 没有那个文件或目录
终于删除干净了,我以为这样愉快的安装部署Python3了。可是,想法太天真了......,当执行 修改安装路径 命令时,报错找不到gcc,通过gcc -v发现没有安装gcc和make插件,于是执行了yum install gcc gcc-c++,报错如下:
[root-3.8.1]# yum install gcc gcc-c++ Python
bash: /usr/bin/yum: /usr/bin/python: 坏的解释器: 没有那个文件或目录
这是因为yum依赖python2。因此,删除python2肯定使用不了yum,给自己埋了一个大坑啊。好吧,自己造的孽自己解决吧,于是把yum也给卸载了,重新安装......
卸载删除yum前查询一下:
[ ]
yum: /usr/bin/yum /etc/yum /etc/yum.conf /usr/share/man/man8/yum.8
[ ]
/usr/bin/yum
[ ]
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch
yum-metadata-parser-1.1.4-10.el7.x86_64
yum-3.4.3-168.el7.centos.noarch
yum-utils-1.1.31-54.el7_8.noarch
yum-langpacks-0.4.2-7.el7.noarch
PackageKit-yum-1.1.10-2.el7.centos.x86_64
[ ]
bash: /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/*
[ ]
CentOS Linux release 7.9.2009 (Core)
[ ]
64
http://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/
lvm2-python-libs-2.02.187-6.el7.x86_64.rpm
libxml2-python-2.9.1-6.el7.5.x86_64.rpm
yum-plugin-aliases-1.1.31-54.el7_8.noarch.rpm
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
yum-plugin-protectbase-1.1.31-54.el7_8.noarch.rpm
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-3.4.3-168.el7.centos.noarch.rpm
yum-utils-1.1.31-54.el7_8.noarch.rpm
python-chardet-2.2.1-3.el7.noarch.rpm
python-kitchen-1.1.1-5.el7.noarch.rpm
python-setuptools-0.9.8-7.el7.noarch.rpm
python-urlgrabber-3.10-10.el7.noarch.rpm
python-pycurl-7.19.0-19.el7.x86_64.rpm
python-iniparse-0.4-9.el7.noarch.rpm
python-2.7.5-89.el7.x86_64.rpm
python-backports-1.0-8.el7.x86_64.rpm
python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch.rpm
python-ipaddress-1.0.16-2.el7.noarch.rpm
python-libs-2.7.5-89.el7.x86_64.rpm
rpm-python-4.11.3-45.el7.x86_64.rpm
rpm -Uvh --replacepkgs lvm2-python-libs*.rpm --nodeps --force
rpm -Uvh --replacepkgs libxml2-python*.rpm --nodeps --force
rpm -Uvh --replacepkgs python*.rpm --nodeps --force
rpm -Uvh --replacepkgs rpm-python*.rpm yum*.rpm --nodeps --force
[root@localhost /]# python --version
Python 2.7.5
[root@localhost rpm-bak]# which python
/usr/bin/python
[root@localhost rpm-bak]# whereis python
python: /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 python
python-setuptools-0.9.8-7.el7.noarch
python-backports-1.0-8.el7.x86_64
python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch
python-ipaddress-1.0.16-2.el7.noarch
rpm-python-4.11.3-45.el7.x86_64
libxml2-python-2.9.1-6.el7.5.x86_64
python-urlgrabber-3.10-10.el7.noarch
lvm2-python-libs-2.02.187-6.el7.x86_64
python-pycurl-7.19.0-19.el7.x86_64
python-2.7.5-89.el7.x86_64
python-kitchen-1.1.1-5.el7.noarch
python-libs-2.7.5-89.el7.x86_64
python-chardet-2.2.1-3.el7.noarch
python-iniparse-0.4-9.el7.noarch
[root@localhost /]# yum install gcc gcc-c++
已加载插件:aliases, fastestmirror, protectbase
Loading mirror speeds from cached hostfile
There 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>
[ ]
[ ]
总用量 0
那么我就使用阿里和网易的吧,设置过程如下:
# 下载阿里和网易源到/etc/yum.repos.d/
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget 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 all
yum makecache
# 查看系统可用的yum源和所有的yum源
yum repolist enabled
哎,yum依赖Python2而存在,而删除自带的Python2是有风险的。上面的一顿操作,相当于恢复之前的删除系统自带Python2环境。踩的坑挺深,这个以后要注意啦!!!
[root@localhost bin]# ll python*
lrwxrwxrwx. 1 root root 7 7月 24 20:59 python -> python2
lrwxrwxrwx. 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 --version
Python 2.7.5
[root@localhost bin]# python --version
Python 2.7.5
就是说,python指向的是python2,而python2又指向了python2.7,终端输入python和python2都可以用到python2.7.5的环境,这里我们把python的指向改为python3,就可以实现python2和python3共存了。
之前那篇文章《》需要改动的是软连接创建目标路径,把 /usr/local/bin/python 改成 /usr/bin/python 即可。先把之前的软连接删除掉:
[ ]
/usr/local/bin
[ ]
lrwxrwxrwx. 1 root root 31 7月 24 21:44 python -> /usr/local/python38/bin/python3
[ ]
[ ]
ls: 无法访问python*: 没有那个文件或目录
[ ]
lrwxrwxrwx. 1 root root 28 7月 24 21:44 pip -> /usr/local/python38/bin/pip3
[ ]
[ ]
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/python
ln: 无法创建符号链接"/usr/bin/python": 文件已存在
[root@localhost bin]# ll python*
lrwxrwxrwx. 1 root root 7 7月 24 20:59 python -> python2
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]# 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/python3
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 /]# python --version
Python 3.8.1
[root@localhost /]# python2 --version
Python 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 7月 24 22:51 pip3 -> /usr/local/python38/bin/pip3
[root@localhost bin]# pip3 list
Package Version
---------- -------
pip 21.1.3
setuptools 41.2.0
这里只要为Python3设置pip3就行了,Python2不再设置对应的pip。
下载第三方库,通过 pip3 install 名称 即可:
以上是关于Linux踩坑系列:删除系统自带python2环境引发的问题的主要内容,如果未能解决你的问题,请参考以下文章
linux安装Python3并与系统自带的Python2并存