ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2020-08-31 21:00:19  阅读:370  来源: 互联网

标签:None Python3.7 local openssl ssl usr 模块 安装 connect


背景:

今天在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导致的。解决方案都是一样的。

 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最新版本

1 wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz

 

2.编译安装openssl

 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.备份原来的配置

 mv /usr/bin/openssl /usr/bin/openssl.bak
 mv /usr/include/openssl/ /usr/include/openssl.bak

 

4.配置新版本的链接

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.修改系统配置

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

 

6.查看openssl版本

openssl version

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

 

7. 配置环境变量

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文件

1 ./configure --enable-shared  --with-openssl=/usr/local/openssl

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

 

 

9.安装Python

1 make
2 
3 make install

 

 

10.验证是否成功

 

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

 

标签:None,Python3.7,local,openssl,ssl,usr,模块,安装,connect
来源: https://www.cnblogs.com/yuxiuyan/p/13591854.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有