CentOS7 升级 python3 过

2020-01-06 15:33:24 浏览数 (1)

CentOS7 升级 python3 过程及注意

代码语言:javascript复制
检查当前的版本
[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、python2、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模块:

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

代码语言:javascript复制
例如这样: 

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

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

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

Type “help”, “copyright”, “credits” or “license” for more information.

import ssl Traceback (most recent call last): File “”, line 1, in File “/usr/local/python27/lib/python2.7/ssl.py”, line 60, in 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 修改成下面:
代码语言:javascript复制
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安装

0 人点赞