CentOS7 升級 python3 過程及注意

CentOS7 升級 python3 過程及注意

檢查當前的版本
[root@node1 ~]# python -V
Python 2.7.5
建立安裝目錄(自定義)
[root@node1 Python-3.7.1]# mkdir /usr/local/python3

從官網下載python3的壓縮包,解壓(以3.7.1版本爲例)
# wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz
# tar zxf Python-3.7.1.tgz 

cd 進入解壓目錄,而後
# cd Python-3.7.1
# ./configure --prefix=/usr/local/python3/
# make && make install

cd 進入/usr/bin
其中有python、python二、python2.7三個文件依次指向後者。
備份當前默認版本python,若是有須要還可還原:
# sudo mv python python.bak

建立python3.7的新連接(也可創建python3命令以區分,同mac),這樣默認的python版本就替換爲python3.7了
# ln -s /usr/local/python3/bin/python3.7 /usr/bin/python

檢查當前默認python版本
# python -v

因爲yum使用python2,替換爲python3後沒法正常工做,
所以修改yum配置文件:
# sudo vi /usr/bin/yum
將第一行指定的python版本改成python2.7:
*#!/usr/bin/python 改成 #!/usr/bin/python2.7

修改urlgrabber配置文件(網上不少教程都漏了這一步)
# sudo vi /usr/libexec/urlgrabber-ext-down
同yum,把頭部的python改爲python2.7

連接:https://www.jianshu.com/p/74227d7ae6a6

問題

Python安裝完畢後,提示找不到ssl模塊:node

(<http://blog.csdn.net/qq_25560423/article/details/62055497>;)python

例如這樣: 

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 

Collecting xxx 

Could not fetch URL <https://pypi.python.org/simple/xxxx/>: There was a problem confirming the ssl certificate: Can’t connect to HTTPS URL because the SSL module is not available. - skipping 

Could not find a version that satisfies the requirement xxx (from versions: ) 

No matching distribution found for xxx

[root@localhost ~]# python2.7.5 linux

Python 2.7.5 (default, Jun 3 2013, 11:08:43) bash

[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2 python2.7

Type 「help」, 「copyright」, 「credits」 or 「license」 for more information.socket

import ssl ide

Traceback (most recent call last): 測試

File 「」, line 1, in fetch

File 「/usr/local/python27/lib/python2.7/ssl.py」, line 60, in ui

import _ssl # if we can’t import it, let the error propagate

ImportError: No module named _ssl


  1. 查看openssl安裝包,發現缺乏openssl-devel包

    [root@localhost ~]# rpm -aq|grep openssl

    openssl-0.9.8e-20.el5

    openssl-0.9.8e-20.el5

    [root@localhost ~]#

  2. yum安裝openssl-devel

    [root@localhost ~]# yum install openssl-devel -y

    查看安裝結果

    [root@localhost ~]# rpm -aq|grep openssl

    openssl-0.9.8e-26.el5_9.1

    openssl-0.9.8e-26.el5_9.1

    openssl-devel-0.9.8e-26.el5_9.1

    openssl-devel-0.9.8e-26.el5_9.1

  3. 從新編譯python

    修改Setup文件

    vi /usr/software/Python-2.7.5/Modules/Setup

    修改爲下面:

Socket module helper for socket(2)

_socket socketmodule.c timemodule.c

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
  1. 從新編譯

    make

    make install

  2. 測試,已可正常使用。

    [root@localhost ~]# python2.7.5

    Python 2.7.5 (default, Jun 3 2013, 14:56:13)

    [GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2

    Type 「help」, 「copyright」, 「credits」 or 「license」 for more information.

    import ssl

從新用pip安裝

相關文章
相關標籤/搜索