解决Python找不到ssl模块问题 No module named _ssl的方法
python安装完毕后,提示找不到ssl模块:
[www@pythontab.com~]$python Python2.7.15(default,Oct232018,18:08:43) [GCC4.4.720120313(RedHat4.4.7-23)]onlinux2 Type"help","copyright","credits"or"license"formoreinformation. >>>importssl Traceback(mostrecentcalllast): File"",line1,in File"/usr/local/python27/lib/python2.7/ssl.py",line60,in import_ssl#ifwecan'timportit,lettheerrorpropagate ImportError:Nomodulenamed_ssl >>>
解决方法:
1.查看openssl安装包,发现缺少openssl-devel包
[www@pythontab.com~]$rpm-aq|grepopenssl openssl-0.9.8e-20.el5 openssl-0.9.8e-20.el5 [www@pythontab.com~]$
2.yum安装openssl-devel
[www@pythontab.com~]$yuminstallopenssl-devel-y #查看安装结果 [www@pythontab.com~]$rpm-aq|grepopenssl openssl-devel-1.0.1e-57.el6.x86_64 openssl-1.0.1e-57.el6.x86_64
3.重新编译python
修改Setup文件
vi/src/Python-2.7.15/Modules/Setup
修改结果如下:
#Socketmodulehelperforsocket(2) _socketsocketmodule.ctimemodule.c #SocketmodulehelperforSSLsupport;youmustcommentouttheother #socketlineabove,andpossiblyedittheSSLvariable: #SSL=/usr/local/ssl _ssl_ssl.c\ -DUSE_SSL-I$(SSL)/include-I$(SSL)/include/openssl\ -L$(SSL)/lib-lssl-lcrypto
4.重新编译
进入源码目录,重新编译安装
[www@pythontab.com~]$cd/src/Python-2.7.15/ [www@pythontab.com~]$make [www@pythontab.com~]$makeinstall
5.测试,已可正常使用。
[www@pythontab.com~]$python Python2.7.15(default,Oct232018,19:08:43) [GCC4.4.720120313(RedHat4.4.7-23)]onlinux2 Type"help","copyright","credits"or"license"formoreinformation. >>>importssl >>>
注:如需保留旧版本的就不需要执行6.7两部
6重命名旧版本的python依赖
ll/usr/bin|greppython mv/usr/bin/python/usr/bin/python2.7
7删除旧的软链接,创建新的软链接到最新的python
rm-rf/usr/bin/python ln-s/usr/local/bin/python3.6/usr/bin/python python-V
使用yum命令报错File"/usr/bin/yum",line30exceptKeyboardInterrupt,e:
问题出现原因:
yum包管理是使用python2.x写的,将python2.x升级到python3.1.3以后,由于python版本语法兼容性导致问题出现
解决办法:
修改yum配置文件,将python版本指向以前的旧版本
#vi/usr/bin/yum #!/usr/bin/python2.7 修改urlgrabber-ext-down文件,更改python版本 #vi/usr/libexec/urlgrabber-ext-down #!/usr/bin/python2.7 CouldnotfetchURLhttps://pypi.python.org/simple/six/:Therewasaproblemconfirmingthesslcertificate:Can'tconnecttoHTTPSURLbecausetheSSLmoduleisnotavailable.-skipping
如需安装pip
下载相关文件
curlhttps://bootstrap.pypa.io/get-pip.py-oget-pip.py
执行
/usr/local/python/bin/python3get-pip.py
添加环境变量
vim~/.bash_profile
添加下面这条参数
exportPATH=/usr/local/python/bin:$PATH
保存
source~/.bash_profile
测试
执行
[root@huo~]#python3 Python3.6.5(default,Apr12018,20:41:34) [GCC4.8.520150623(RedHat4.8.5-16)]onlinux Type"help","copyright","credits"or"license"formoreinformation. >>>
执行脚本如下:
viminstall_python.sh
#!/bin/bash echo"正在安装相关组件" yuminstall-yopenssl-develbzip2-develexpat-develgdbm-develreadline-develsqlite-develgcc-c++gccopenssl-devel echo"下载安装包" wgethttps://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz echo"正在解压安装包" tar-xfPython-3.6.5.tgz-C/root/&&cd/root/Python-3.6.5/ echo"添加ssl支持" cat>>/root/Python-3.6.5/Modules/Setup.dist<<"EOF" _socketsocketmodule.c SSL=/usr/local/ssl _ssl_ssl.c\ -DUSE_SSL-I$(SSL)/include-I$(SSL)/include/openssl\ -L$(SSL)/lib-lssl-lcrypto EOF echo"正在编译安装Python" ./configure--prefix=/usr/local/python&&make&&makeinstall cd/root echo"删除安装包" rm-rf/root/Python-3.6.5.tgz&&rm-rf/root/Python-3.6.5 echo"正在添加环境变量" echo"exportPATH=/usr/local/python/bin:$PATH">>~/.bash_profile source~/.bash_profile echo"安装完成,请执行python3进行测试"
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。