Centos下升级Python

发布时间:2019-09-19 08:00:29编辑:auto阅读(1797)

    Centos下升级Python


    另一篇文章 Centos7安装Python3.7(兼容Python2.7)https://blog.51cto.com/leyex/2163465

    预准备(可忽略)

    由于Centos需要提前安装Sqlite数据库,否则之后Python无法正常导入sqlite3,因此建议在升级Python前预先安装Sqlite数据库

    没有安装readline-devel可能导致无法使用键盘的上下左右键

    yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y


    一、查看本机Pyhton版本

    本机Centos6.5 预带Pyhton版本为2.6.6

    # python --version
    >>Python 2.6.6


    二、下载欲升级最新Python安装包

    可以去官网找最新安装包 https://www.python.org/downloads/ 


    三、修改编译文件支持SSL

    编译安装的时候并没有把SSL模块编译进去,安装后可能会导致urllib2不支持https链接

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/urllib2.py", line 154, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/local/lib/python2.7/urllib2.py", line 431, in open
        response = self._open(req, data)
      File "/usr/local/lib/python2.7/urllib2.py", line 454, in _open
        'unknown_open', req)
      File "/usr/local/lib/python2.7/urllib2.py", line 409, in _call_chain
        result = func(*args)
      File "/usr/local/lib/python2.7/urllib2.py", line 1265, in unknown_open
        raise URLError('unknown url type: %s' % type)
    urllib2.URLError: <urlopen error unknown url type: https>

    我们需要先安装openssl-devel(已安装可忽略)

    # rpm -aq|grep openssl
    >>openssl-1.0.1e-30.el6.x86_64
    # yum install openssl-devel -y
    # rpm -aq|grep openssl
    >>openssl-1.0.1e-30.el6.x86_64
    >>openssl-devel-1.0.1e-42.el6.x86_64

    在编译安装Python之前加上以下这段代码(代码去除注释即可)

    >>vi Python-2.7.11/Modules/Setup.dist
    # Socket module helper for SSL support; you must comment out the other
    # socket line above, and possibly edit the SSL variable:
    SSL=/usr/local/ssl
    _ssl _ssl.c \
            -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
            -L$(SSL)/lib -lssl -lcrypto

    *另注:

    由于系统缺少了readline相关模块,会导致编译安装Python后无法使用上下左右退格,所以需要再编译安装前安装readline-devel模块

    yum -y install readline-devel


    、编译安装Python

    wget  
    tar -zxvf Python-2.7.10.tgz
    cd Python-2.7.10
    ./configure 
    make && make install
    #也可以先查看是否有安装gcc再进行编译安装

    通过命令查看是否已安装新版本

    # /usr/local/bin/python2.7 -V
    >>Python 2.7.11

    现在我们可以通过python2.7启动python,但是这样太麻烦,我们可以自行修改下

    mv /usr/bin/python /usr/bin/python2.6.6
    ln -s /usr/local/bin/python2.7 /usr/bin/python
    # python -V
    >>Python 2.7.11


    、修改yum脚本环境变量引用

    安装完新版本Python后会发现yum出错,所以我们要把yum指向老版本

    vi /usr/bin/yum
    #!/usr/bin/python2.6.6 # 修改#!/usr/bin/python为#!/usr/bin/python2.6.6
    import sys
    try:
        import yum
    except ImportError:
        print >> sys.stderr, """\
    .....

    测试yum命令,一切正常


    、升级完Python后安装pip

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

    or

    python从2.6升级到2.7之后会出现安装setuptools和pip启动失败,这是因为路径的问题,我们可以下载一个脚本,运行后会自动重新下载一个setuptools

    curl -O https://bootstrap.pypa.io/ez_setup.py

    # python ez_setup.py

    安装setuptools

    # yum install -y python-setuptools

    安装distribute

    由于Setuptools包不再维护distribute,所以安装后可能由于distribute版本缺失导致报错

    # easy_install
    Traceback (most recent call last):
      File "/usr/local/bin/easy_install", line 5, in <module>
        from pkg_resources import load_entry_point
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2675, in <module>
        parse_requirements(__requires__), Environment()
      File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 552, in resolve
        raise DistributionNotFound(req)
    pkg_resources.DistributionNotFound: distribute==0.6.10 distribution was not found and is required by the application

    通过以下方式安装缺失的distribute

    # wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.10.zip --no-check-certificate
    # unzip distribute-0.6.10.zip
    # cd distribute-0.6.10
    # /usr/local/bin/python2.7 setup.py install

    or

    # wget 
    # tar xf distribute-0.6.10.tar.gz
    # cd distribute-0.6.10
    # /usr/local/bin/python2.7 setup.py install


    安装pip

    # easy_install pip

    # easy_install -i https://pypi.tuna.tsinghua.edu.cn/simple pip

    七、升级完Python后ibus输入法报错

    升级 Python2.7 后会导致 Ibus 输入法python2.6的gtk无法使用问题

    /usr/libexec/ibus-ui-gtk
    /usr/ bin/ibus-setup
    /usr/libexec/ibus-engine-table

    分别修改以上三个文件中的  “python”  为  “python2.6”

    八、配置pip和easy_isntall的镜像源


    easy_install的配置文件 ~/.pydistutils.cfg :

    [easy_install]
    index_url = https://pypi.tuna.tsinghua.edu.cn/simple


    pip的配置文件 ~/.pip/pip.conf :

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple


    至此,升级完毕,后续继续补充



    本文仅作一个记录,以备忘记查看方便



关键字