[转帖]Python3 ssl模块不可用的问题

python3,ssl,模块,不可,问题 · 浏览次数 : 0

小编点评

问题原因: pip 安装 numpy 时,由于缺少 OpenSSL 头文件和库文件,导致 `_ssl` 模块不可找到。 解决方案: 1. **安装 OpenSSL 库:** ``` sudo yum install openssl-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel ``` 2. **重新编译 pip 库:** ``` pip install --upgrade pip ``` 3. **重新安装 numpy:** ``` sudo pip uninstall numpy sudo pip install numpy ``` **注意:** * `numpy-1.14.5-cp36-cp36m-manylinux1_x86_64.whl` 是 numpy 的最新版本,可能存在与您系统版本不匹配的版本错误。请确保您使用的是与您的系统版本匹配的版本。 * 如果您无法使用 `sudo` 命令,您可以使用 `pip install numpy` 的其他选项,例如 `--user` 或 `--no-binary`。

正文

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

 

编译安装完Python3之后,使用pip来安装python库,发现了如下报错:

复制代码
$ pip install numpy
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting numpy
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/numpy/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/numpy/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/numpy/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/numpy/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)': /simple/numpy/
  Could not fetch URL https://pypi.python.org/simple/numpy/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.python.org', port=443): Max retries exceeded with url: /simple/numpy/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
  Could not find a version that satisfies the requirement numpy (from versions: )
No matching distribution found for numpy
复制代码

网上说了一种解决方案,是在./configure 的时候,加上--with-ssl选项,然后重新编译安装,尝试了下:

$ ./configure --with-ssl
... ...
configure: WARNING: unrecognized options: --with-ssl
... ...

出了个警告:不可识别的--with-ssl选项。

./configure --help看了下确实也没发现这个选项,估计是版本不一致,不大想折腾这个版本问题了,决定换个思路。

尝试安装openssl:

$ sudo yum install openssl

安装成功之后,重新编译安装,依旧报这个错,但是在make的时候有了一些发现:

复制代码
$ make
... ...
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _ssl                  _tkinter           
readline                                    
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
复制代码

可以看到,这里虽然make成功了,但是报了很多模块缺失,查看下编译安装目录下的setup.py,搜索_ssl,可以定位到如下代码:

复制代码
 843         # Detect SSL support for the socket module (via _ssl)
 844         search_for_ssl_incs_in = [
 845                               '/usr/local/ssl/include',
 846                               '/usr/contrib/ssl/include/'
 847                              ]
 848         ssl_incs = find_file('openssl/ssl.h', inc_dirs,
 849                              search_for_ssl_incs_in
 850                              )
 851         if ssl_incs is not None:
 852             krb5_h = find_file('krb5.h', inc_dirs,
 853                                ['/usr/kerberos/include'])
 854             if krb5_h:
 855                 ssl_incs += krb5_h
 856         ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
 857                                      ['/usr/local/ssl/lib',
 858                                       '/usr/contrib/ssl/lib/'
 859                                      ] )
 860 
 861         if (ssl_incs is not None and
 862             ssl_libs is not None):
 863             exts.append( Extension('_ssl', ['_ssl.c'],
 864                                    include_dirs = ssl_incs,
 865                                    library_dirs = ssl_libs,
 866                                    libraries = ['ssl', 'crypto'],
 867                                    depends = ['socketmodule.h']), )
 868         else:
 869             missing.append('_ssl')
复制代码

可以看到,setup.py会在'/usr/local/ssl/include', '/usr/contrib/ssl/include/' 这两个目录里面搜索'openssl/ssl.h' 这个头文件,然后会在 '/usr/local/ssl/lib' 和 '/usr/contrib/ssl/lib/' 之中搜索 ssl 的 lib文件,搜索不到,会将_ssl加入missing这个数组里面,然后寻找missing调用的地方:

复制代码
 313         if missing:
 314             print()
 315             print("Python build finished successfully!")
 316             print("The necessary bits to build these optional modules were not "
 317                   "found:")
 318             print_three_column(missing)
 319             print("To find the necessary bits, look in setup.py in"
 320                   " detect_modules() for the module's name.")
 321             print()
复制代码

找到了上面报错时候的输出,很明显,是由于搜索不到ssl.h头文件或者搜索不到lib文件而导致的报错,但是我刚刚明明是装了openssl的啊,为啥还会报找不到呢?手动搜索下:

$ sudo find / -name ssl.h

没找到ssl.h,折腾了一番之后,找到了如下命令:

$ sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel 

可以看到,这个命令安装的是openssl-devel,与我之前安装的openssl有所不同,查阅资料之后发现,openssl只包含了可执行部分,openssl-devel才包含了头文件、头文件参考、某些库文件等跟开发相关的东西。所以只安装openssl是找不到相应的头文件的,安装完之后,再次编译:

复制代码
$ make clean
$ make
... ...
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_dbm                  _gdbm                 _lzma              
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
复制代码

果然发现,缺失的模块少了很多。

继续安装:

$ sudo make install

安装完之后,重新执行pip:

$ pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/68/1e/116ad560de97694e2d0c1843a7a0075cc9f49e922454d32f49a80eb6f1f2/numpy-1.14.5-cp36-cp36m-manylinux1_x86_64.whl (12.2MB)
    6% |██                              | 747kB 10kB/s eta 0:17:52

至此,pip安装报错的问题解决。

 
分类: python
标签: python3pipssl

与[转帖]Python3 ssl模块不可用的问题相似的内容:

[转帖]Python3 ssl模块不可用的问题

https://www.cnblogs.com/minglee/p/9232673.html 编译安装完Python3之后,使用pip来安装python库,发现了如下报错: $ pip install numpy pip is configured with locations that requi

[转帖]unrecognized options: --with-ssl

解决办法: vi Moudel/Setup 找到如下内容 去掉209--212行的注释 再次执行编译 ./configure prefix=/usr/local/python3 文章知识点与官方知识档案匹配,可进一步学习相关知识网络技能树首页概览22965 人正在系统学习中

[转帖]The necessary bits to build these optional modules were not found: _uuid _bz2 _curse _curses_panel

在安装Python3.7可能遇到如题的错误,只需安装uuid库就可以 ubuntu下安装uuid链接库 sudo apt-get install uuid-dev CentOS yum install libuuid-devel 对于以下的问题 The necessary bits to build

[转帖]KeenTune(轻豚)

Dependencies 安装python3运行环境 yum install python36 python36-devel pip3 install --upgrade pip 安装python依赖包 pip3 install hyperopt==0.2.5 pip3 install numpy=

[转帖]Linux下编译安装配置python3.9

Linux版本:CentOS-7.8-x86_64-Minimal-2003 操作用户:root (1)依赖包安装: 如果是Linux的minimal系统,需要安装: yum install -y vim wget tftp lrzsz bzip2 zip unzip net-tools bind-

[转帖]py_innodb_page_info.py工具使用

目录 一、Linux安装Python3 1. 解压包 2. 安装环境 3. 生成编译脚本 4. 检查python3.10的编译器 5. 建立Python3和pip3的软链 6. 添加到PATH 7. 验证Python3和pip3是否正常 二、py_innodb_page_info.py工具 1. p

[转帖]Python基础之数据类型(一)

https://www.jianshu.com/p/5c43f6056f77 Python基本数据类型 python系列文档都是基于python3 数字 字符串 列表 元组 字典 集合 一、数字 特性: 只能存放一个值 一经定义,不可更改 直接访问 整型十进制转二进制bin(10)十进制转八进制oc

[转帖]Python基础之文件处理(二)

https://www.jianshu.com/p/7dd08066f499 Python基础文件处理 python系列文档都是基于python3 一、字符编码 在python2默认编码是ASCII, python3里默认是utf-8; unicode分为 utf-32(占4个字节),utf-16(

[转帖]Python-Curses模块

Python-Curses 什么是Curses?Python Curses模块开始我们的编程吧(启动与结束)重要使用方法(包装用法)Windows and Pads显示文本属性和颜色用户输入 什么是Curses? curses库为基于文本的终端提供独立于终端的屏幕绘制和键盘处理设施;这些终端包括VT

[转帖]python 性能优化监控工具_Linux性能优化(四)——BCC性能监控工具

一、BCC简介 1、BCC简介 BCC是一个Python库,简化了eBPF应用的开发过程,并收集了大量性能分析相关的eBPF应用。BCC为BPF开发提供了不同的前端支持,包括Python和Lua,实现了map创建、代码编译、解析、注入等操作,使开发人员只需聚焦于用C语言开发要注入的内核代码。 BCC