Centos 7从python2.7.5

发布时间:2019-06-30 16:53:12编辑:auto阅读(1826)

    第一步:查看Centos版本及python版本

    • Centos版本
    [root@qsh ~]# cat /etc/centos-release
    CentOS Linux release 7.6.1810 (Core)
    • python版本
    [root@qsh ~]# python -V
    Python 2.7.5
    [root@qsh usr]# ll -l /usr/bin/python*
    lrwxrwxrwx 1 root root    7 3月  29 22:44 /usr/bin/python -> python2
    lrwxrwxrwx 1 root root    9 3月  29 22:44 /usr/bin/python2 -> python2.7
    -rwxr-xr-x 1 root root 7136 11月  6 00:29 /usr/bin/python2.7

    第二步:从官网下载python对应版本的包并解压

    [root@qsh packages]# wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
    [root@qsh packages]# tar zxvf Python-2.7.15.tgz

    第三步:配置、编译、安装

    • 安装gcc(在编译时会依赖)
    yum install gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel python-devel zlib
    • 配置、编译、安装
    cd Python-2.7.15
    ./configure --prefix=/usr/local  #  [配置]指定可执行文件、库文件、配置文件、资源文件的安装路径。若没有权限加sudo
    make  # 编译
    make altinstall  # 不要使用make install,否则会覆盖系统自带python

    第四步:安装后环境检查

    • python安装后的版本
    [root@qsh ~]# python -V  # 发现版本还是原版本Python 2.7.5
    • 安装前后的python对比
    [root@qsh ~]# ll -l /usr/bin/python*  # 系统自带的
    lrwxrwxrwx 1 root root    7 2019/03/03 14:24:13 /usr/bin/python -> python2
    lrwxrwxrwx 1 root root    9 2019/03/03 14:24:13 /usr/bin/python2 -> python2.7
    -rwxr-xr-x 1 root root 7216 2018/10/31 07:46:45 /usr/bin/python2.7
    -rwxr-xr-x 1 root root 1835 2018/10/31 07:46:26 /usr/bin/python2.7-config
    lrwxrwxrwx 1 root root   16 2019/03/26 17:13:24 /usr/bin/python2-config -> python2.7-config
    lrwxrwxrwx 1 root root   14 2019/03/26 17:13:24 /usr/bin/python-config -> python2-config
    
    [root@qsh ~]# ll -l /usr/local/bin/python*  # 手工安装的
    -rwxr-xr-x 1 root root 8296384 2019/03/26 17:16:56 /usr/local/bin/python2.7
    -rwxr-xr-x 1 root root    1687 2019/03/26 17:17:05 /usr/local/bin/python2.7-config
    • 备份旧版本,连接新版本
    [root@qsh ~]# mv /usr/bin/python /usr/bin/python2.7.5
    [root@qsh ~]# ll -l /usr/bin/python*
    lrwxrwxrwx 1 root root    9 2019/03/03 14:24:13 /usr/bin/python2 -> python2.7
    -rwxr-xr-x 1 root root 7216 2018/10/31 07:46:45 /usr/bin/python2.7
    lrwxrwxrwx 1 root root    7 2019/03/03 14:24:13 /usr/bin/python2.7.5 -> python2  # 改为2.7.5
    -rwxr-xr-x 1 root root 1835 2018/10/31 07:46:26 /usr/bin/python2.7-config
    lrwxrwxrwx 1 root root   16 2019/03/26 17:13:24 /usr/bin/python2-config -> python2.7-config
    lrwxrwxrwx 1 root root   14 2019/03/26 17:13:24 /usr/bin/python-config -> python2-config
    
    [root@qsh ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python # 增加连接
    
    [root@qsh ~]# ll -l /usr/bin/python*
    lrwxrwxrwx 1 root root   24 2019/03/26 17:20:18 /usr/bin/python -> /usr/local/bin/python2.7  # 新增的,并指向新安装的python
    lrwxrwxrwx 1 root root    9 2019/03/03 14:24:13 /usr/bin/python2 -> python2.7
    -rwxr-xr-x 1 root root 7216 2018/10/31 07:46:45 /usr/bin/python2.7
    lrwxrwxrwx 1 root root    7 2019/03/03 14:24:13 /usr/bin/python2.7.5 -> python2
    -rwxr-xr-x 1 root root 1835 2018/10/31 07:46:26 /usr/bin/python2.7-config
    lrwxrwxrwx 1 root root   16 2019/03/26 17:13:24 /usr/bin/python2-config -> python2.7-config
    lrwxrwxrwx 1 root root   14 2019/03/26 17:13:24 /usr/bin/python-config -> python2-config
    • 再次检查python版本
    [root@qsh ~]# python -V
    Python 2.7.15
    • 若想访问老版本python(如2.7.5版本)
    [root@qsh ~]# python2.7.5 -V
    Python 2.7.5

    第五步:yum的设置(系统预装的yum引用的老版本python)

    [root@qsh ~]# yum -y install epel-release
    There was a problem importing one of the Python modules
    required to run yum. The error leading to this problem was:
    
       No module named yum  #这时候报错了
    
    Please install a package which provides this module, or
    verify that the module is installed correctly.
    
    It's possible that the above module doesn't match the
    current version of Python, which is:
    2.7.13 (default, Apr  2 2017, 04:48:29)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
    
    If you cannot solve this problem yourself, please go to
    the yum faq at:
      http://yum.baseurl.org/wiki/Faq
    
    #解决方法
    [root@qsh ~]# vi /usr/bin/yum
    首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
    
    改完之后继续安装,又报错...
    ImportError: No module named urlgrabber.grabber
    
    [root@qsh ~]# vi /usr/libexec/urlgrabber-ext-down
    首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7


    第六步:升级 python 后,安装 pip 工具

    1. 下载安装

    wget https://bootstrap.pypa.io/get-pip.py
    python get-pip.py


    2. 设置软连接

    ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip

     

    #安装 ipython

    pip install ipython==1.2.1
    ln -s /usr/local/python27/bin/ipython /usr/bin/ipython


关键字