美文网首页
Centos7安装Python3,并配置与Python2共存

Centos7安装Python3,并配置与Python2共存

作者: Monky | 来源:发表于2020-10-09 09:09 被阅读0次

    一、下载python3系的安装包

    二、上传、解压安装包

    1. 先在/usr/local/目录下,新建python3文件夹,用来存放和安装python
    [root@localhost ~]# mkdir /usr/local/python3
    
    1. 进入,python3目录,使用rz命令上传安装包(我用的是xshell,使用rz命令会自动调用资源管理选择上传安装包,如果你用的是虚拟机,建议用在线下载的模式。先进入到python3文件夹,然后使用上面的wget命令进行下载)
    [root@localhost ~]# cd /usr/local/python3/
    [root@localhost python3]# rz
    
    1. 解压安装包
    [root@localhost python3]# tar -xvf Python-3.8.3.tgz
    
    1. 接下来处理环境问题,使用下面命令,安装python3需要的环境
    [root@localhost python3]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
    
    1. 环境安装完成后,对python源码包进行编译
    [root@localhost python3]# cd Python-3.8.3
    [root@localhost Python-3.8.3]# ./configure prefix=/usr/local/python3
    
    1. 编译完成后,进行安装
    [root@localhost Python-3.8.3]# make && make install
    
    1. 编译完成,需要对python3进行软链接创建,首先我们查看python2的安装信息,并对python2软链接进行修改
    [root@localhost Python-3.8.3]# 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
    # 可以看到python2 安装在 /usr/bin 目录下,进入该目录,查看相关信息
    
    [root@localhost Python-3.8.3]# cd /usr/bin/
    [root@localhost bin]# ll python*
    
    lrwxrwxrwx. 1 root root    7 9月   4 2019 python -> python2
    lrwxrwxrwx. 1 root root    9 9月   4 2019 python2 -> python2.7
    -rwxr-xr-x. 1 root root 7216 10月 31 2018 python2.7
    # 可以看到,python指向python2,python2又指向python2.7,所以我们需要修改python指向python3即可,原先python2指向python2.7不做变动。这样,python2和python3就能共存了
    
    1. 备份python指向python2 的软连接
    [root@localhost bin]# mv /usr/bin/python /usr/bin/python.bak
    [root@localhost bin]# ll python*
    
    lrwxrwxrwx. 1 root root    9 9月   4 2019 python2 -> python2.7
    -rwxr-xr-x. 1 root root 7216 10月 31 2018 python2.7
    lrwxrwxrwx. 1 root root    7 9月   4 2019 python.bak -> python2
    
    
    1. 创建python3 的软链接
    [root@localhost bin]# ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
    # 注意python3的安装目录
    
    [root@localhost bin]# ll python*
    lrwxrwxrwx. 1 root root   32 6月  28 17:36 python -> /usr/local/python3/bin/python3.8
    lrwxrwxrwx. 1 root root    9 9月   4 2019 python2 -> python2.7
    -rwxr-xr-x. 1 root root 7216 10月 31 2018 python2.7
    lrwxrwxrwx. 1 root root    7 9月   4 2019 python.bak -> python2
    # 可以看到,python已经指向了python3.8
    
    1. 更改yum配置,因为该命令是基于python2执行的,不修改的话会导致yum不能正常使用
    [root@localhost bin]# vim /usr/bin/yum
    首行,把 #! /usr/bin/python修改为 #! /usr/bin/python2
      
    [root@localhost bin]# vim /usr/libexec/urlgrabber-ext-down
    首行,把 #! /usr/bin/python 修改为 #! /usr/bin/python2
    
    1. 验证
    [root@localhost bin]# python2
    Python 2.7.5 (default, Oct 30 2018, 23:45:53) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
    [root@localhost bin]# python
    Python 3.8.3 (default, Jun 28 2020, 17:26:01) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
    

    相关文章

      网友评论

          本文标题:Centos7安装Python3,并配置与Python2共存

          本文链接:https://www.haomeiwen.com/subject/gfwsuktx.html