我尝试使用sklearn的fetch_olivetti_faces数据集,但是由于某些原因,如果可以帮助我,我将无法解决此IOError:

这是我运行的代码:

from sklearn import datasets
data = datasets.fetch_olivetti_faces()

这是我得到的IOError:
IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

这是我得到的所有错误:
File "/anaconda3/lib/python2.7/site-packages/sklearn/datasets/olivetti_faces.py", line 120, in fetch_olivetti_faces
mat_path = _fetch_remote(FACES, dirname=data_home)

File "/anaconda3/lib/python2.7/site-packages/sklearn/datasets/base.py", line 874, in _fetch_remote
urlretrieve(remote.url, file_path)

File "/anaconda3/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)

File "/anaconda3/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)

File "/anaconda3/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)

File "/anaconda3/lib/python2.7/urllib.py", line 443, in open_https
h.endheaders(data)

File "/anaconda3/lib/python2.7/httplib.py", line 1038, in endheaders
self._send_output(message_body)

File "/anaconda3/lib/python2.7/httplib.py", line 882, in _send_output
self.send(msg)

File "/anaconda3/lib/python2.7/httplib.py", line 844, in send
self.connect()

File "/anaconda3/lib/python2.7/httplib.py", line 1263, in connect
server_hostname=server_hostname)

File "/anaconda3/lib/python2.7/ssl.py", line 363, in wrap_socket
_context=self)

File "/anaconda3/lib/python2.7/ssl.py", line 611, in __init__
self.do_handshake()

File "/anaconda3/lib/python2.7/ssl.py", line 840, in do_handshake
self._sslobj.do_handshake()

IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

最佳答案

您可以通过导入ssl软件包并通过以下行执行以下命令来解决问题:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

请注意,这将禁用证书验证,从而有效地关闭SSL。此解决方案在开发环境中很好,但是必须在使用任何面向公众的地方之前整理出证书。

10-06 02:34