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

背景:

今天在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,1)"> 6   Retrying (Retry(total=2,1)"> 7   Retrying (Retry(total=1,1)"> 8   Retrying (Retry(total=0,1)"> 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,1)">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/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/8 ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

 

5.修改系统配置

1 #写入openssl库文件的搜索路径
echo /usr/local/openssl/lib" >> /etc/ld.so.conf
4 #使修改后的/etc/.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/include3     export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig"

 

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

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

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

 

 

9.安装Python

2 
3 install

 

 

10.验证是否成功

 

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

 

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...