Ubuntu, python, SSL.SSLError: [SSL: CERTIFICATE_VERIFY_FAILE



我正在运行一些示例代码来测试在GPU上运行的Tensorflow的安装。Python工作正常,我可以导入Tensorflow而没有任何错误。

但是,当我尝试从 TensorFlow 网站加载数据集时,我遇到了问题,并收到以下错误消息。

该问题与某些SSL证书有关,对此我知之甚少,因此我正在寻求帮助。

(相关( 代码

import tensorflow as tf
(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()

错误消息

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>
...
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>
...
Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

完整回溯

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
Traceback (most recent call last):
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1254, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1300, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1249, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1036, in _send_output
self.send(msg)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 974, in send
self.connect()
File "/home/filip/anaconda3/envs/tf/lib/python3.6/http/client.py", line 1415, in connect
server_hostname=server_hostname)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 817, in __init__
self.do_handshake()
File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/home/filip/anaconda3/envs/tf/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/filip/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/utils/data_utils.py", line 251, in get_file
urlretrieve(origin, fpath, dl_progress)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 248, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 526, in open
response = self._open(req, data)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 544, in _open
'_open', req)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 504, in _call_chain
result = func(*args)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 1361, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/home/filip/anaconda3/envs/tf/lib/python3.6/urllib/request.py", line 1320, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/filip/PycharmProjects/mist_test/mist_test_file.py", line 2, in <module>
(x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()
File "/home/filip/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/datasets/mnist.py", line 49, in load_data
file_hash='8a61469f7ea1b51cbae51d4f78837e45')
File "/home/filip/anaconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/utils/data_utils.py", line 255, in get_file
raise Exception(error_msg.format(origin, e.errno, e.reason))
Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)

系统信息

# packages in environment at /home/filip/anaconda3/envs/tf:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
absl-py                   0.8.0                    pypi_0    pypi
astor                     0.8.0                    pypi_0    pypi
ca-certificates           2019.5.15                     1  
certifi                   2019.6.16                py36_1  
gast                      0.3.1                    pypi_0    pypi
google-pasta              0.1.7                    pypi_0    pypi
grpcio                    1.23.0                   pypi_0    pypi
h5py                      2.10.0                   pypi_0    pypi
keras-applications        1.0.8                    pypi_0    pypi
keras-preprocessing       1.1.0                    pypi_0    pypi
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 9.1.0                hdf63c60_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
markdown                  3.1.1                    pypi_0    pypi
ncurses                   6.1                  he6710b0_1  
numpy                     1.17.2                   pypi_0    pypi
openssl                   1.1.1d               h7b6447c_0  
pip                       19.2.2                   py36_0  
protobuf                  3.9.1                    pypi_0    pypi
python                    3.6.9                h265db76_0  
readline                  7.0                  h7b6447c_5  
setuptools                41.0.1                   py36_0  
six                       1.12.0                   pypi_0    pypi
sqlite                    3.29.0               h7b6447c_0  
tensorboard               1.14.0                   pypi_0    pypi
tensorflow-estimator      1.14.0                   pypi_0    pypi
tensorflow-gpu            1.14.0                   pypi_0    pypi
termcolor                 1.1.0                    pypi_0    pypi
tk                        8.6.8                hbc83047_0  
werkzeug                  0.15.6                   pypi_0    pypi
wheel                     0.33.4                   py36_0  
wrapt                     1.11.2                   pypi_0    pypi
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               h7b6447c_3

您可以在下载之前添加以下代码。解决了从互联网上下载类似错误的问题。有关更多详细信息,请参阅此链接

import requests
requests.packages.urllib3.disable_warnings()
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy Python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context

最新更新