设置yum共存多个python版本

centos7.5或7.6最小化进行镜像默认安装python2.7.5,但现在很多第三方库或者个人项目都使用python3.5以后,例如TensorFlow只支持python3.5以上,这里主要记录在服务器上如何实现共存python多版本(通过更换软连接方式实现),同时也不会影响yum运行报错(yum使用python2.7.5),内容很基础。

1、原环境python版本

[[email protected] ~]# python -V
Python 2.7.5
# 查看当前python路径

[[email protected] ~]# python -V
Python 2.7.5
[[email protected] ~]# which python
/usr/bin/python

2、安装python3

直接通过yum安装python36,指定版本为python3.6,也可指定其他版本

yum install gcc gcc-c++ -y
# 安装python3.6版本
yum -y install python36 python36-devel
# 或安装python3.7版本
yum -y install python37 python37-devel

或者通过编译安装python36

yum install gcc gcc-c++ -y
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz
#解压
tar -Jxf Python-3.6.8.tar.xz 

[[email protected] ~]# cd Python-3.6.8
#编译安装前的配置参数说明,编译前带上一部分选项,这里设置的安装目录
[[email protected] Python-3.6.8]# ./configure --help
[[email protected] Python-3.6.8]# ./configure --prefix=/usr/bin/
[[email protected] Python-3.6.8]# make && make install 

# 
首先找到pip3的位置(在python3.6的安装包内)
cd /usr/local/bin
ln -s /usr/local/bin/pip3.6 /bin/pip3       
#创建pip3命令的连接符
pip3 -v
完成!

查看新版本

[[email protected] ~]# python3 -V
Python 3.6.8
[[email protected] ~]# python36 -V
Python 3.6.8

3、将python3设置为服务器默认版本

查看python2的路径

[[email protected] ~]# ls -al /usr/bin | grep python2
lrwxrwxrwx.  1 root root          7 Jun 27 11:59 python -> python2
lrwxrwxrwx.  1 root root          9 Jun 27 11:59 python2 -> python2.7
-rwxr-xr-x.  1 root root       7216 Jun 21 04:28 python2.7

上面清晰看到软链接的连接流程,在shell敲上python时,软链接到python2,python2继续软链接python2.7,这种软链接方式在很多地方可以看到,都是为了方便运行相关命令。

再看python3的软链接

[[email protected] ~]# ls -al /usr/bin | grep python3
lrwxrwxrwx.  1 root root          9 Aug 22 17:09 python3 -> python3.6
lrwxrwxrwx.  1 root root         18 Aug 22 17:09 python36 -> /usr/bin/python3.6
-rwxr-xr-x.  2 root root      11408 Apr 26 05:05 python3.6
lrwxrwxrwx.  1 root root         17 Aug 22 17:09 python3.6-config -> python3.6m-config
-rwxr-xr-x.  2 root root      11408 Apr 26 05:05 python3.6m
-rwxr-xr-x.  1 root root        173 Apr 26 05:04 python3.6m-config
-rwxr-xr-x.  1 root root       3435 Apr 26 04:44 python3.6m-x86_64-config
lrwxrwxrwx.  1 root root         16 Aug 22 17:09 python3-config -> python3.6-config
# 在shell输入不同python3软连接,都是指向/usr/bin/python3.6解释器
[[email protected] ~]# python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[[email protected] ~]# python36
Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[[email protected] ~]# python3.6
Python 3.6.8 (default, Apr 25 2019, 21:02:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

以上软链接流程可知,只需将/usr/bin/python3 软链接到 /usr/bin/python ,那么在shell执行python命令时,自然就链接到python3,pyhton3软链接到pyhton3.6。

替换软链接

mv /usr/bin/python /usr/bin/python.old
将python3软链接到python
ln -s /usr/bin/python3 /usr/bin/python

以上完成默认python3版本的配置

4、解决yum运行报错

上面已经将系统python环境配置python3,而yum的代码还是python2的格式,因此

[[email protected] ~]# yum list  
File "/usr/bin/yum", line 30     except KeyboardInterrupt, e:
SyntaxError: invalid syntax
# 这就是python2的exception语法,python3为 except KeyboardInterrupt as e:

这是因为yum的安装脚本其代码的头文件第一行为 #!/usr/bin/python,从上面可知,这个路径是指向python3

[[email protected] ~]# vi /usr/bin/yum
#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

解决:

文件1:/usr/bin/yum和

文件2:/usr/libexec/urlgrabber-ext-down,

将以上 文件头行#!/usr/bin/python 改为 #!/usr/bin/python2.7,保存退出即可

5、卸载yum安装的python3版本

这里也给出如何卸载通过yum安装的python3版本

# 卸载python3安装包
[[email protected] lib]# rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps 
Preparing packages...
python36-devel-3.6.8-1.el7.x86_64
python3-rpm-macros-3-25.el7.noarch
python36-3.6.8-1.el7.x86_64
python36-libs-3.6.8-1.el7.x86_64
# 删除所有python3的关联文件
[[email protected]~]# whereis python3 |xargs rm -frv
removed ‘/opt/py36/bin/python3.6’
removed ‘/opt/py36/bin/python3’
发布了52 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/pysense/article/details/100772519