前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux编译安装安Python3.7/3.8出现_ssl模块错误| python运行ssl模块出现ModuleNotFoundError

Linux编译安装安Python3.7/3.8出现_ssl模块错误| python运行ssl模块出现ModuleNotFoundError

作者头像
于果
发布2021-08-25 16:13:51
7.7K2
发布2021-08-25 16:13:51
举报
文章被收录于专栏:效能与质量效能与质量

背景:

今天在Linux上使用paramiko模块的时候,出现了错误:ModuleNotFoundError:No module name '_ssl',但是我的系统是安装了openssl的1.0.1的,查了网络上的信息发现,Python3.7以后的版本,需要openssl1.0.2+,或者Libressl2.6.4+。

按照网络上的方法,安装了openssl-1.1.1g,对Python3.8重新手动编译安装,但是在执行make命令的时候仍旧提示_ssl模块没有被成功导入。经过查询,发现是LDFLAGS,CPPFLAGS,PKG_CONFIG_PATH这几个环境变量的问题。

LDFLAGS:gcc 等编译器会用到的一些优化参数,也可以在里面指定库文件的位置。用法:LDFLAGS=-L/usr/lib -L/path/to/your/lib。每安装一个包都几乎一定的会在安装目录里建立一个lib目录。如果明明安装了某个包,而安装另一个包时,它愣是说找不到,可以把那个包的lib路径加入的LDFALGS中试一下。

CPPFLAGS:CXXFLAGS=$CFLAGS 。CFLAGS 表示用于 C 编译器的选项,CXXFLAGS 表示用于 C++ 编译器的选项。这两个变量实际上涵盖了编译和汇编两个步骤。大多数程序和库在编译时默认的优化级别是”2″(使用”-O2″选项)并且带有调试符号来编 译,也就是 CFLAGS=”-O2 -g”,.

PKG_CONFIG_PATH:它指定pkg-config将在其中搜索其.pc文件的其他路径。此变量用于增强pkg-config的默认搜索路径。在典型的Unix系统上,它将搜索目录/usr/lib/pkgconfig/usr/share/pkgconfig。这通常包括系统安装的模块。但是,某些本地模块可能安装在不同的前缀中,例如/usr/local。在这种情况下,必须预先设置搜索路径,以便pkg-config可以找到.pc文件。pkg-config程序用于检索有关系统中已安装库的信息。 pkg-config的主要用途是提供编译程序和链接到库的必要细节。此元数据存储在pkg-config文件中。这些文件具有后缀.pc,并位于pkg-config工具已知的特定位置。

还有可能在使用pip安装的时候,报错ssl module in Python is not available,这些本质上都是因为Python在编译安装的时候,没有找到合适版本的ssl导致的。解决方案都是一样的。

代码语言:javascript
复制
 1 [root@localhost ~]# /usr/local/python3/bin/pip3 install paramiko
 2 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
 3 Collecting virtualenv
 4   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/virtualenv/
 5   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/virtualenv/
 6   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/virtualenv/
 7   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/virtualenv/
 8   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/virtualenv/
 9   Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenv/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
10   Could not find a version that satisfies the requirement virtualenv (from versions: )
11 No matching distribution found for virtualenv
12 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
13 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

解决方案:

需要升级openssl版本>=1.0.2或者Libressl>=2.6.4,然后对Python3.8重新编译安装。

1.下载openssl最新版本

代码语言:javascript
复制
1 wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz

2.编译安装openssl

代码语言:javascript
复制
 1 tar -zxvf openssl-1.1.1g.tar.gz  #解压
 2 
 3 cd openssl-1.1.1g
 4 
 5 ./config –prefix=/usr/local/openssl   no-zlib #安装到这个路径
 6 
 7 
 8 make
 9 
10 make install

3.备份原来的配置

代码语言:javascript
复制
 mv /usr/bin/openssl /usr/bin/openssl.bak
 mv /usr/include/openssl/ /usr/include/openssl.bak

4.配置新版本的链接

代码语言:javascript
复制
1 #将安装好的openssl 的openssl命令软连到/usr/bin/openssl
2 ln -s /usr/local/openssl/include/openssl /usr/include/openssl
3 
4 #软链到升级后的libssl.so
5 ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
6 
7 #将安装好的openssl命令软连到/usr/bin/openssl
8 ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

5.修改系统配置

代码语言:javascript
复制
1 #写入openssl库文件的搜索路径
2 echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
3 
4 #使修改后的/etc/ld.so.conf生效 
5 ldconfig -v

6.查看openssl版本

代码语言:javascript
复制
openssl version

如果安装成功的话,这时候会显示你安装的版本。

7. 配置环境变量

代码语言:javascript
复制
1     export LDFLAGS=" -L/usr/local/openssl/lib"
2     export CPPFLAGS=" -I/usr/local/openssl/include"
3     export PKG_CONFIG_PATH="/usr/local/openssl/lib/pkgconfig"

8.解压python安装包,执行configure文件

代码语言:javascript
复制
1 ./configure --enable-shared  --with-openssl=/usr/local/openssl

这时候需要检查一下最后的ssl配置是否正常,

9.安装Python

代码语言:javascript
复制
1 make
2 
3 make install

10.验证是否成功

现在已经不报错了,说明安装成功了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景:
  • 解决方案:
    • 1.下载openssl最新版本
      • 2.编译安装openssl
        • 3.备份原来的配置
          • 4.配置新版本的链接
            • 5.修改系统配置
              • 6.查看openssl版本
                • 7. 配置环境变量
                  • 8.解压python安装包,执行configure文件
                    • 9.安装Python
                      • 10.验证是否成功
                      相关产品与服务
                      SSL 证书
                      腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档