Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

前言

本文描述手动编译 python3.10 缺少 openssl 模块问题解决方案和几个小时解决这个问题的全过程。虽然我编译的是 python3.10,但是对于 python3.6、python3.8 应该同样适用。

解决方案

给新手说一下,我的服务器环境是 centos7,你们如果是其他环境,不要奇怪为啥某些命令用不了,百度一下。下面进入正题:

安装 openssl-1.1.1

  1. wget https://www.openssl.org/source/openssl-1.1.1n.tar.gz –no-check-certificate 下载openssl1.1.1
  2. tar -zxf openssl-1.1.1n.tar.gz 解压
  3. cd openssl-1.1.1n
  4. ./Configure –prefix=/usr/local/openssl 设置安装目录 可以自定义 但是要记住,后面会用到
  5. make -j && make install 编译并安装

重新编译 python3.10

  1. 切换到 python3.10 解压包目录
  2. 已经编译过的可以先 make clean 清理一下
  3. ./configure –prefix=/usr/local/python3 –with-openssl=/usr/local/openssl –with-openssl-rpath=auto 标粗的这个一定别漏了,血泪教训
  4. make -j && make install
  5. 到这就安装好了,如果只是解决问题可以不往下看了。

详细过程

  1. pip3 search anyHub 看一下有没有这个库
    报错 Caused by SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)

  2. -_-||, 之前编译vim8 需要python3,装了几个版本的python3 ,但是ssl这个报错都存在,看来得先把这个解决,不然不能保证python3是完整可用的。

  3. 看网上说是openssl版本对不上,yum update -y openssl openssl-devel 升级一下openssl

  4. 现在就要重新编译 python3.10 了,因为我之前试过3.8、3.6 都会报错,所以干脆就搞最新的。

  5. 编译要带编译选项,我就想看下之前编译时传了哪些参数,避免影响其他功能了,那怎么查看已经编译好的python 用了什么编译选项呢?

  6. 这里有几种方法:

    1. python3 -m sysconfig | less 这展示了 python 所有环境变量,太多了,我要的是显示我手动传递的编译参数,换下一个。

    2. import sysconfig
      print(sysconfig.get_config_vars()) 麻蛋 也是显示所有的

    3. import sysconfig
      print(sysconfig.get_config_vars(“CONFIG_ARGS”)) 终于对了,我这里就传了一个编译参数
      Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  7. 还有个准备工作,我应该加什么编译参数? 因为我印象中我之前编译的时候,加过各种各样带有 openssl 的编译选项,但是都没啥卵用。例如:–with-openssl=/usr/local/openssl/lib

  8. 那怎么查看 python 支持的编译选项?
    ./configure –help
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  9. 但是我并不知道这个参数是传 openssl 的什么目录,网上查了一通,有人说什么都不跟,那就试一下先,结果报错如下:
    ./configure –prefix=/usr/local/python3 –with-opensslCaused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  10. 搜一下报错代码,发现 with-openssl 必须赋值
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  11. 通过后续使用 ssldirs 的代码可以看出这里传递的目录名应该包含以下文件:

    1. xxx/include/openssl/ssl.h  头文件 xxx 为 –with-openssl=xxx 中的路径信息
    2. xxx/lib/libssl.so 和 xxx/lib/libcrypto.so 动态库文件
      Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解
  12. 大致了解了传递的 path 需要具备什么样的子目录,那么就先到官网下一个最新版的 openssl-3.0.0

    1. wget https://www.openssl.org/source/openssl-3.0.2.tar.gz –no-check-certificate

    2. ./Configure 报错
      Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

    3. 百度解决该报错,需要安装 perl-CPAN

      1. yum install -y perl-CPAN
      2. perl -MCPAN -e shell
      3. cpan[1]> install IPC/Cmd.pm
      
  13. ./Configure –prefix=/usr/local/openssl 再次配置

  14. make && make install

  15. 查看安装好的文件目录发现 /usr/local/openssl 下面没有 lib 目录,只有 lib64,但是我们上面提到,该路径下必须有 xxx/lib/libssl.so ,那么我们手动 copy 出来一个:
    cp -rf /usr/local/openssl/lib64 /usr/local/openssl/lib

  16. 重新编译 pyton3.10

    1. ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
    2. make -j  编译,但是报错了
    

    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  17. 根据报错信息分析下:(分析个锤子,要是多往上面看几行就好了)

    1. 当前不能构建ssl module 那么就是3.0版本不行哦 (信你个鬼,看完后续大家可以试试就用3.0,毕竟3.0官网说的支持到26年,1.1.1支持到23年11月份)
    2. 需要 1.1.1 或者更新 ,yum 看一下我们之前的openssl版本,果然只有1.0.2, 那么下一个 1.1.1 的来试试吧。
      Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解
  18. 和前面安装 openssl-3.0 步骤一样,只是链接改成 https://www.openssl.org/source/openssl-1.1.1n.tar.gz ,编译好了 openssl 1.1.1 ,再来编译python,又报错说找不到libssl.so.1.1:
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  19. 这次报错信息就很明确了,找不到对应的库 libssl.so.1.1。我严重怀疑之前 3.0 这里也是报的这个错误信息,大家可以自己试试。

  20. 但是俺们这路径下是有该文件的:
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  21. 这时候在把 python 支持的关于 openssl 的编译选项翻出来看,发现漏了个关键信息
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解
    哎,英语不好,不喜欢看一大段话的描述,漏掉了这个大鱼,rpath默认为 no,如果不手动设置为 auto, –with-openssl 选项设置了也没用,设置了再来试试。

  22. ./configure –prefix=/usr/local/python3 –with-openssl=/usr/local/openssl –with-openssl-rpath=auto
    这里插一句 可以用 make -j 加快编译,不要 make && make install, 因为这种不阻断流程的错误信息,你就看不到了,我最开始就是 -_-||

  23. 编译成功,至少这次没有刚才的关于 openssl 的错误信息了,现在 make Install 安装
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  24. 使用新的pip3 再来搜索下 anyHub ,又有报错,不过已经不是 openssl 引发的了:
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  25. 百度了下发现是要安装 pip_search 包,pip3 install pip_search 安装下包

  26. pip_search anyHub 报错:找不到命令
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

  27. 因为没有把 /usr/local/python3/bin 加到path里面,所以只能显示调用安装位置的 pip_search 程序,到这里为止,带openssl 模块的 python3.10 可以确认是安装好了
    Caused by SSLError(“Can‘t connect to HTTPS URL because the SSL module is not available.“) 详解

结语

耐心看手册,细心看日志,可以少走一些弯路。

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年3月12日
下一篇 2023年3月12日

相关推荐