解決Python找不到ssl模塊問題 No module named _ssl的方法
python安裝完畢后,提示找不到ssl模塊:
[www@pythontab.com ~]$ python Python 2.7.15 (default, Oct 23 2018, 18:08:43) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ssl Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python27/lib/python2.7/ssl.py", line 60, in <module> import _ssl # if we can't import it, let the error propagate ImportError: No module named _ssl >>>
解決方法:
1. 查看openssl安裝包,發(fā)現缺少openssl-devel包
[www@pythontab.com ~]$ rpm -aq|grep openssl openssl-0.9.8e-20.el5 openssl-0.9.8e-20.el5 [www@pythontab.com ~]$
2. yum安裝openssl-devel
[www@pythontab.com ~]$ yum install openssl-devel -y #查看安裝結果 [www@pythontab.com ~]$ rpm -aq|grep openssl 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
修改結果如下:
# Socket module helper for socket(2) _socket socketmodule.c timemodule.c # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: #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 ~]$ make install
5. 測試,已可正常使用。
[www@pythontab.com ~]$ python Python 2.7.15 (default, Oct 23 2018, 19:08:43) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>>
注:如需保留舊版本的就不需要執(zhí)行 6 .7兩部
6 重命名舊版本的python依賴
ll /usr/bin | grep python mv /usr/bin/python /usr/bin/python2.7
7 刪除舊的軟鏈接,創(chuàng)建新的軟鏈接到最新的python
rm -rf /usr/bin/python ln -s /usr/local/bin/python3.6 /usr/bin/python python -V
使用yum命令報錯File "/usr/bin/yum", line 30 except KeyboardInterrupt, 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 Could not fetch URL https://pypi.python.org/simple/six/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
如需安裝pip
下載相關文件
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
執(zhí)行
/usr/local/python/bin/python3 get-pip.py
添加環(huán)境變量
vim ~/.bash_profile
添加下面這條參數
export PATH=/usr/local/python/bin:$PATH
保存
source ~/.bash_profile
測試
執(zhí)行
[root@huo ~]# python3 Python 3.6.5 (default, Apr 1 2018, 20:41:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
執(zhí)行腳本如下:
vim install_python.sh
#!/bin/bash echo "正在安裝相關組件" yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc-c++ gcc openssl-devel echo "下載安裝包" wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz echo "正在解壓安裝包" tar -xf Python-3.6.5.tgz -C /root/ && cd /root/Python-3.6.5/ echo "添加ssl支持" cat >> /root/Python-3.6.5/Modules/Setup.dist <<"EOF" _socket socketmodule.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 && make install cd /root echo "刪除安裝包" rm -rf /root/Python-3.6.5.tgz && rm -rf /root/Python-3.6.5 echo "正在添加環(huán)境變量" echo "export PATH=/usr/local/python/bin:$PATH">> ~/.bash_profile source ~/.bash_profile echo "安裝完成,請執(zhí)行python3進行測試"
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
如何使用Python連接?SSH?服務器并執(zhí)行命令
實際開發(fā)中,有時候經常需要查看日志,有時候使用ssh工具打開就為了看一下錯誤日志又比較麻煩,所以今天帶來一個簡單的基于python的小工具,感興趣的朋友跟隨小編一起看看吧2023-11-11