在Cent0S 7.5下将Python 2.7.5升级到Python 3.6.6后,发现ssl模块不可用,具体详细信息如下所示:

[root@db-server ~]# pip list

Package    Version

---------- -------

pip        19.2.3 

setuptools 39.0.1 

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

Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping

 

[root@db-server ~]# python -V

Python 3.6.6

 

>>> import ssl

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/usr/local/lib/python3.6/ssl.py", line 101, in <module>

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

ModuleNotFoundError: No module named '_ssl'

>>> 

>>> import socket

>>> hasattr(socket,"SSL")

False

>>>

检查发现openssl包已经安装了,然后按照网上的文章,修改Modules/Setup.dist中,找到SSL配置部分,如下截图所示

[root@db-server ~]# yum list installed |grep openssl

openssl.x86_64                        1:1.0.2k-19.el7                  @base    

openssl-libs.x86_64                   1:1.0.2k-19.el7                  @base 

# 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

# The crypt module is now disabled by default because it breaks builds

# on many systems (where -lcrypt is needed), e.g. Linux (I believe).

#

# First, look at Setup.config; configure may have set this for you.

#_crypt _cryptmodule.c # -lcrypt        # crypt(3); needs -lcrypt on some systems

这里需要取消注释部分(上图红框附近部分的设置),设置SSL路径,但是这个SSL的安装路径在哪里呢? 我查找了一下,发现openssl的安装路径如下:

[root@db-server ~]# whereis openssl

openssl: /usr/bin/openssl /usr/lib64/openssl /usr/share/man/man1/openssl.1ssl.gz

[root@db-server ~]# rpm -ql openssl

/etc/pki/CA

/etc/pki/CA/certs

/etc/pki/CA/crl

/etc/pki/CA/newcerts

/etc/pki/CA/private

/etc/pki/tls/certs/Makefile

/etc/pki/tls/certs/make-dummy-cert

/etc/pki/tls/certs/renew-dummy-cert

/etc/pki/tls/misc/CA

/etc/pki/tls/misc/c_hash

/etc/pki/tls/misc/c_info

/etc/pki/tls/misc/c_issuer

/etc/pki/tls/misc/c_name

/usr/bin/openssl

/usr/share/doc/openssl-1.0.2k

/usr/share/doc/openssl-1.0.2k/FAQ

/usr/share/doc/openssl-1.0.2k/NEWS

/usr/share/doc/openssl-1.0.2k/README

/usr/share/doc/openssl-1.0.2k/README.FIPS

/usr/share/doc/openssl-1.0.2k/README.legacy-settings

/usr/share/licenses/openssl-1.0.2k

/usr/share/licenses/openssl-1.0.2k/LICENSE

/usr/share/man/man1/asn1parse.1ssl.gz

/usr/share/man/man1/ca.1ssl.gz

/usr/share/man/man1/ciphers.1ssl.gz

/usr/share/man/man1/cms.1ssl.gz

/usr/share/man/man1/crl.1ssl.gz

/usr/share/man/man1/crl2pkcs7.1ssl.gz

/usr/share/man/man1/dgst.1ssl.gz

/usr/share/man/man1/dhparam.1ssl.gz

/usr/share/man/man1/dsa.1ssl.gz

/usr/share/man/man1/dsaparam.1ssl.gz

/usr/share/man/man1/dss1.1ssl.gz

/usr/share/man/man1/ec.1ssl.gz

/usr/share/man/man1/ecparam.1ssl.gz

/usr/share/man/man1/enc.1ssl.gz

/usr/share/man/man1/errstr.1ssl.gz

/usr/share/man/man1/gendsa.1ssl.gz

/usr/share/man/man1/genpkey.1ssl.gz

/usr/share/man/man1/genrsa.1ssl.gz

/usr/share/man/man1/md2.1ssl.gz

/usr/share/man/man1/md4.1ssl.gz

/usr/share/man/man1/md5.1ssl.gz

/usr/share/man/man1/mdc2.1ssl.gz

/usr/share/man/man1/nseq.1ssl.gz

/usr/share/man/man1/ocsp.1ssl.gz

/usr/share/man/man1/openssl.1ssl.gz

/usr/share/man/man1/pkcs12.1ssl.gz

/usr/share/man/man1/pkcs7.1ssl.gz

/usr/share/man/man1/pkcs8.1ssl.gz

/usr/share/man/man1/pkey.1ssl.gz

/usr/share/man/man1/pkeyparam.1ssl.gz

/usr/share/man/man1/pkeyutl.1ssl.gz

/usr/share/man/man1/req.1ssl.gz

/usr/share/man/man1/ripemd160.1ssl.gz

/usr/share/man/man1/rsa.1ssl.gz

/usr/share/man/man1/rsautl.1ssl.gz

/usr/share/man/man1/s_client.1ssl.gz

/usr/share/man/man1/s_server.1ssl.gz

/usr/share/man/man1/s_time.1ssl.gz

/usr/share/man/man1/sess_id.1ssl.gz

/usr/share/man/man1/sha.1ssl.gz

/usr/share/man/man1/sha1.1ssl.gz

/usr/share/man/man1/sha224.1ssl.gz

/usr/share/man/man1/sha256.1ssl.gz

/usr/share/man/man1/sha384.1ssl.gz

/usr/share/man/man1/sha512.1ssl.gz

/usr/share/man/man1/smime.1ssl.gz

/usr/share/man/man1/speed.1ssl.gz

/usr/share/man/man1/spkac.1ssl.gz

/usr/share/man/man1/sslpasswd.1ssl.gz

/usr/share/man/man1/sslrand.1ssl.gz

/usr/share/man/man1/ts.1ssl.gz

/usr/share/man/man1/verify.1ssl.gz

/usr/share/man/man1/version.1ssl.gz

/usr/share/man/man1/x509.1ssl.gz

/usr/share/man/man5/config.5ssl.gz

/usr/share/man/man5/openssl.cnf.5ssl.gz

/usr/share/man/man5/x509v3_config.5ssl.gz

/usr/share/man/man7/des_modes.7ssl.gz

尝试了几个路径,例如SSL=/usr/lib64/openssl ,然后重新编译安装Python,发现依然报错,

[root@db-server Python-3.6.6]# vi Modules/Setup.dist

SSL=/usr/lib64/openssl

_ssl _ssl.c \

-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \

-L$(SSL)/lib -lssl -lcrypto

#cd /tmp/Python-3.6.6

#./configure --prefix=/usr/local

#make

#make install

在Python的解压安装包里面,我查了一下setup.py , 搜索ssl关键字,发现有如下一些代码, 但是我在系统搜索了一下,居然找不到这些目录(ssl/include和/ssl/lib)和ssl.h这些文件。

[root@db-server ~]# vi /tmp/Python-3.6.6/setup.py

    # Detect SSL support for the socket module (via _ssl)

        search_for_ssl_incs_in = [

                              '/usr/local/ssl/include',

                              '/usr/contrib/ssl/include/'

                             ]

        ssl_incs = find_file('openssl/ssl.h', inc_dirs,

                             search_for_ssl_incs_in

                             )

        if ssl_incs is not None:

            krb5_h = find_file('krb5.h', inc_dirs,

                               ['/usr/kerberos/include'])

            if krb5_h:

                ssl_incs += krb5_h

        ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,

                                     ['/usr/local/ssl/lib',

                                      '/usr/contrib/ssl/lib/'

                                     ] )

 

        if (ssl_incs is not None and

            ssl_libs is not None):

            exts.append( Extension('_ssl', ['_ssl.c'],

                                   include_dirs = ssl_incs,

                                   library_dirs = ssl_libs,

                                   libraries = ['ssl', 'crypto'],

                                   depends = ['socketmodule.h']), )

        else:

            missing.append('_ssl')

[root@db-server ~]# ls -lrt /usr/lib64/openssl

total 0

drwxr-xr-x. 2 root root 218 Sep 20 07:00 engines

 

[root@db-server ~]# ls /usr/local/ssl

ls: cannot access /usr/local/ssl: No such file or directory

 

[root@db-server ~]# find / -name ssl.h

后面才搞清楚,openssl包只包含了可执行部分,openssl-devel才包含了头文件、头文件参考、某些库文件等以及跟开发相关的东西。所以只安装了openssl包是找不到相应的头文件的,安装完openssl-devel之后,验证确认这些目录和文件已经存在了。

[root@db-server ~]# rpm -qa | grep openssl-devel

[root@db-server ~]# rpm -qa | grep openssl

openssl-1.0.2k-19.el7.x86_64

openssl-libs-1.0.2k-19.el7.x86_64

 

[root@db-server ~]# yum list installed |grep openssl-devel

[root@db-server ~]# yum list installed |grep openssl

openssl.x86_64                        1:1.0.2k-19.el7                  @base    

openssl-libs.x86_64                   1:1.0.2k-19.el7                  @base    

[root@db-server ~]# 

 

[root@db-server ~]# yum install openssl-devel

使用# rpm -ql  openssl-devel 定位安装安装路径为“/usr/include/openssl”,修改安装路径的Modules/Setup.dist文件,修改后的部分如下所示(对比上面截图),然后重新编译安装Python后问题彻底解决。

# Socket module helper for SSL support; you must comment out the other

# socket line above, and possibly edit the SSL variable:

SSL=/usr/include/openssl

_ssl _ssl.c \

-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \

-L$(SSL)/lib -lssl –lcrypto

 

 

参考资料:

https://www.cnblogs.com/minglee/p/9232673.html

Python升级后ssl模块不可用问题解决和浅析的更多相关文章

  1. CENTOS下Python 升级后YUM无法使用的解决办法

    Python有很多实用的工具,安装依赖python版本较高,升级Python后导致yum无法使用. 原因: 系统自带的yum依赖Python老版本,升级后不兼容 解决办法: 1. 列出所有版本,确定老 ...

  2. python升级后pip 不可用 卸载pip

    python版本由2.6升级到2.7之后,用pip提示报错 找了一下原因,网上的版本很多.弄来弄去比较麻烦 来点简单粗暴的 1.卸载pip yum remove python-pip 2.下载 cur ...

  3. Python3 ssl模块不可用的问题

    编译安装完Python3之后,使用pip来安装python库,发现了如下报错: $ pip install numpy pip is configured with locations that re ...

  4. 转 Python3 ssl模块不可用的问题

      编译安装完Python3之后,使用pip来安装python库,发现了如下报错:   $ pip install numpy pip is configured with locations tha ...

  5. 如何解决python升级后yum报错

    当我们yum命令的时候,会提示 "File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxEr ...

  6. python升级后带来的几个小问题

    1)python升级带来的yum异常:File "/usr/bin/yum", line 30 原因:这是因为yum采用Python作为命令解释器,这可以从/usr/bin/yum ...

  7. CentOS更换python版本后,yum不可用的问题

    因为yum调用了python,他的启动程序/usr/bin/yum就是一个python脚本 yum是不兼容 Python 2.7的,所以yum不能正常工作,我们需要指定 yum 的Python版本 将 ...

  8. python 升级后正确安装 pip

    由于服务器的python 版本是2.6.6 , 为了使用 twisted 升级至 2.7.13 , 如果此时直接用 yum install python-pip 安装 pip, 则实际pip 会安装在 ...

  9. python安装完毕后,提示找不到ssl模块的解决步骤

    转载自 醇酒醉影 python安装完毕后,提示找不到ssl模块: [root@localhost ~]# python2.7.5 Python 2.7.5 (default, Jun 3 2013, ...

随机推荐

  1. iOS设计模式之:建造者模式Builder Pattern,用于改进初始化参数

    转自:http://www.cnblogs.com/wengzilin/p/4365855.html 本文主要讨论一下iOS中的Builder Pattern.与网上很多版本不同,本文不去长篇大论地解 ...

  2. React Context上下文

    目录 前言 一 context旧版使用步骤 1.1 根组件childContextTypes属性 1.2 根组件getChildContext方法 1.3 子组件contextTypes静态属性 1. ...

  3. Spring Data-Spring整合Hibernate基于JPA规范

    JPA:由 Sun 公司提供了一对对于持久层操作的标准(接口+文档) Hibernate:是 Gavin King 开发的一套对于持久层操作的自动的 ORM 框架. Hibernate JPA:是在 ...

  4. LightOj-1030 Discovering Gold (期望DP)

    You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave c ...

  5. Python中的input你真会吗?

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:一米阳光里的晴天娃娃   python中的input()方法是在控制台可 ...

  6. spring源码学习之设计模式(1)单例模式

    众所周知,单例模式分为饿汉式和懒汉式,昨天在看了<spring5核心原理与30个类手写实战>之后才知道饿汉式有很多种写法,分别适用于不同场景,避免反射,线程不安全问题.下面就各种场景.采用 ...

  7. Python3 数据结构之词频统计(英文)

    import string path = r'C:\Users\Black\Desktop\Walden.txt' with open(path, 'r', encoding='utf-8') as ...

  8. 【性能优化】404- 从 12.67s到1.06s 性能优化实战

    作者:jerryOnlyZRJ 来源:https://juejin.im/post/5b6fa8c86fb9a0099910ac91 本文是对之前同名文章的修正,将所有webpack3的内容更新为we ...

  9. < JAVA - 大作业(2)仿qq即时通讯软件 >

    < JAVA - 大作业(2)仿qq即时通讯软件 > 背景 JAVA上机大作业:设计一个仿qq即时通讯软件 任务简要叙述:设计一款仿QQ的个人用户即时通讯软件,能够实现注册,登陆,与好友聊 ...

  10. 阿里云https证书Apache配置

    最近玩小程序,只支持https协议,于是给网站添加https支持.手上没钱,自己生成的证书不受信任,找到了阿里云的免费证书(PS:证书可以用在其他云主机上,不一定是阿里云的主机).如何购买下载不多说, ...