Tensorflow: Mac + Python 3.6.1:尝试下载 mnist 数据导致 CERTIFICATE_VERIFY_FAILED 错误

创建于 2017-06-16  ·  31评论  ·  资料来源: tensorflow/tensorflow

系统信息

  • 我是否编写了自定义代码(而不是使用 TensorFlow 中提供的股票示例脚本) :否
  • 操作系统平台和发行版(例如,Linux Ubuntu 16.04) :Mac OS X 10.12.5
  • TensorFlow 从(源代码或二进制文件)安装:二进制文件
  • TensorFlow 版本(使用下面的命令) :v1.2.0-rc2-21-g12f033d 1.2.0
  • Bazel 版本(如果从源代码编译) :不适用
  • CUDA/cuDNN 版本:无
  • GPU 型号和内存:不适用
  • 重现的确切命令
$ python3 --version
Python 3.6.1

$ python3 -m virtualenv venv
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in .../venv/bin/python3
Also creating executable in .../venv/bin/python
Installing setuptools, pip, wheel...done.

$ source venv/bin/activate
$ pip install tensorflow
Collecting tensorflow
  Using cached tensorflow-1.2.0-cp36-cp36m-macosx_10_11_x86_64.whl
  ....

$ python
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets("/tmp/data/")
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 401, in wrap_socket
    _context=self, _session=session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 808, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1061, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../venv/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py", line 235, in read_data_sets
    SOURCE_URL + TRAIN_IMAGES)
  File ".../venv/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 208, in maybe_download
    temp_file_name, _ = urlretrieve_with_retry(source_url)
  File ".../venv/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 165, in wrapped_fn
    return fn(*args, **kwargs)
  File ".../venv/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 190, in urlretrieve_with_retry
    return urllib.request.urlretrieve(url, filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/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:749)>

这不会在 TensorFlow 1.1 中重现。

awaiting response builinstall

最有用的评论

这也可能是因为我最近换了一台新机器,它仍然只安装了部分:)

供将来参考:如果您想使用 Python dmg 安装程序,您还必须阅读 Python 3 的自述文件并运行/Applications/Python 3.6/Install Certificates.command bash 脚本来安装更新的证书。

感谢您的快速回复,@aselle!

所有31条评论

您可以尝试使用 python2(py 的默认系统安装)吗? 另外,您是如何使用 brew 或 macports 安装的(我们使用 brew 进行测试,因此更有可能工作)。

我使用 Python 网站上的 dmg 安装程序(不是通过 brew 或 macports)安装了 Python 3.6。

它似乎适用于 Python 2.7.10。 我原以为在 Python 2.x 中 urlretrieve 没有验证服务器证书,但显然它从 2.7.9 开始: https ://docs.python.org/2/library/urllib.html

它适用于 Linux 上的 python 3.4。 我会尝试使用 brew 安装 python3.6。 可能是您使用的 python3 发行版没有数据证书。

这也可能是因为我最近换了一台新机器,它仍然只安装了部分:)

供将来参考:如果您想使用 Python dmg 安装程序,您还必须阅读 Python 3 的自述文件并运行/Applications/Python 3.6/Install Certificates.command bash 脚本来安装更新的证书。

感谢您的快速回复,@aselle!

如果您只想将粘贴复制到终端中:
/Applications/Python\ 3.6/Install\ Certificates.command

我在 MacOSX 10.11.6 上遇到了同样的问题

我首先完全按照此处为 Python 3.n 设置virtualenv的步骤进行操作: https ://www.tensorflow.org/install/install_mac

然后,按照本教程https://www.tensorflow.org/get_started/mnist/beginners 中的前两个步骤,我看到以下内容; 任何解决此问题的建议(例如,我可以从其他地方下载吗?)将不胜感激。

(tensorflow) ~/tensorflow  $ python
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 401, in wrap_socket
    _context=self, _session=session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 808, in __init__
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1061, in do_handshake
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 683, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/aaron/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py", line 235, in read_data_sets
    SOURCE_URL + TRAIN_IMAGES)
  File "/Users/aaron/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 208, in maybe_download
    temp_file_name, _ = urlretrieve_with_retry(source_url)
  File "/Users/aaron/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 165, in wrapped_fn
    return fn(*args, **kwargs)
  File "/Users/aaron/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 190, in urlretrieve_with_retry
    return urllib.request.urlretrieve(url, filename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.6/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:748)>

@brightbytes-dude 您是否运行了此处列出的命令: https :

/Applications/Python\ 3.6/Install\ Certificates.command

成功了,谢谢!! (我愚蠢地认为这仅与dmg安装有关。)

您能否将 contrib/learn/python/learn/datasets/mnist.py 中的 MNIST 下载 URL 更改为不使用 https ? 这将帮助遇到此问题的人。 在浏览器中尝试过,http 下载链接有效。

要更改的行:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '
新队:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '

你好,
我是 python 和 tensorflow 的新手。 尝试 MNIST 示例并收到与 paulcwatts 大致相同的错误消息(上图)

从 tensorflow.examples.tutorials.mnist 导入 input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
回溯(最近一次调用最后一次):
文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 1318 行,在 do_open 中
encode_chunked=req.has_header('传输编码'))
请求中的文件“C:UsersAndreasDocumentsPythonlibhttpclient.py”,第 1239 行
self._send_request(method, url, body, headers, encode_chunked)
_send_request 中的文件“C:UsersAndreasDocumentsPythonlibhttpclient.py”,第 1285 行
self.endheaders(主体,encode_chunked=encode_chunked)
文件“C:UsersAndreasDocumentsPythonlibhttpclient.py”,第 1234 行,在 endheaders 中
self._send_output(message_body, encode_chunked=encode_chunked)
_send_output 中的文件“C:UsersAndreasDocumentsPythonlibhttpclient.py”,第 1026 行
self.send(msg)
文件“C:UsersAndreasDocumentsPythonlibhttpclient.py”,第 964 行,发送
self.connect()
文件“C:UsersAndreasDocumentsPythonlibhttpclient.py”,第 1400 行,在连接中
server_hostname=server_hostname)
文件“C:UsersAndreasDocumentsPythonlibssl.py”,第 401 行,在 wrap_socket
_context=self,_session=session)
文件“C:UsersAndreasDocumentsPythonlibssl.py”,第 808 行,在 __init__ 中
self.do_handshake()
文件“C:UsersAndreasDocumentsPythonlibssl.py”,第 1061 行,在 do_handshake 中
self._sslobj.do_handshake()
文件“C:UsersAndreasDocumentsPythonlibssl.py”,第 683 行,在 do_handshake 中
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:748)

在处理上述异常的过程中,又发生了一个异常:

回溯(最近一次调用最后一次):
文件 ”",第 1 行,在
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
文件“C:UsersAndreasDocumentsPythonlibsite-packagestensorflowcontriblearnpythonlearndatasetsmnist.py”,第240行,在read_data_sets中
source_url + TRAIN_IMAGES)
文件“C:UsersAndreasDocumentsPythonlibsite-packagestensorflowcontriblearnpythonlearndatasetsbase.py”,第208行,在maybe_download
temp_file_name,_ = urlretrieve_with_retry(source_url)
文件“C:UsersAndreasDocumentsPythonlibsite-packagestensorflowcontriblearnpythonlearndatasetsbase.py”,第165行,在wrapped_fn
返回 fn( args, * kwargs)
文件“C:UsersAndreasDocumentsPythonlibsite-packagestensorflowcontriblearnpythonlearndatasetsbase.py”,第 190 行,在 urlretrieve_with_retry
返回 urllib.request.urlretrieve(url, 文件名)
文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 248 行,在 urlretrieve 中
使用 contextlib.closure(urlopen(url, data)) 作为 fp:
文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 223 行,在 urlopen 中
返回 opener.open(url, data, timeout)
文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 526 行,打开
response = self._open(req, data)
_open 中的文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 544 行
'_open',请求)
_call_chain 中的文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 504 行
结果 = func(*args)
https_open 中的文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 1361 行
上下文=self._context,check_hostname=self._check_hostname)
文件“C:UsersAndreasDocumentsPythonliburllibrequest.py”,第 1320 行,在 do_open 中
引发 URLError(err)
urllib.error.URLError:

主要区别在于我在 WINDOWS 10 上,我找不到证书命令。
抱歉,可能会问“愚蠢的问题”,但这些错误不会“消失”

我对 ssl 有同样的问题,这条线修复了它,
/Applications/Python 3.6/Install Certificates.command
谢谢@EthanAI @aselle @paulcwatts

如果有人在此页面上磕磕绊绊,但正在寻找在 Ubuntu 中允许 Tensorflow Hub 的答案,我让它像这样工作:

# Directory where user certs can be stored
cd /usr/local/share/ca-certificates
# Below may require elevated permissions, such as sudo su
pip3 install --upgrade certifi  # Installs a certificate file
# Move the installed cert to the place Ubuntu expects it
cp /home/ubuntu/.local/lib/python3.5/site-packages/certifi/cacert.pem .
# Convert it into the format Ubuntu expects
openssl x509 -in cacert.pem -inform pem -outform der -out cacert.crt
# Actually load the new cert into the OS
update-ca-certificates

最重要的是,我的公司代理配置、重新启动 Jupyter notebook 内核以及从终端上的脚本而不是从笔记本运行 TF Hub 的一些神奇组合最终导致 TF Hub 在终端和笔记本上都能工作.

运行该命令也失败了。
./安装证书。命令
-- pip install --upgrade 证书
领取证书
缓存条目反序列化失败,条目被忽略
无法获取 URL https://pypi.python.org/simple/certifi/ :确认 ssl 证书时出现问题:[SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败 (_ssl.c:777) - 跳过
找不到满足证书要求的版本(来自版本:)
没有找到证书的匹配分布
回溯(最近一次调用最后一次):
文件 ”",第 44 行,在
文件 ”",第 25 行,主要内容
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”,第 291 行,在 check_call
引发 CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: 命令 '['/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6', '-E', '-s', '-m', 'pip', 'install' , '--upgrade', 'certifi']' 返回非零退出状态 1。

在 MacOS 上/Applications/Python\ 3.6/Install\ Certificates.command命令对我有用

/Applications/Python\ 3.6/Install\ Certificates.command在 MacOSX High Sierra 上发挥了神奇的作用。 谢谢

对于那些使用虚拟环境的人来说,解决方案是什么?

更新:看起来运行上面指出的相同系统命令👆 也可以在活动的 venv 中使用。 很高兴知道!

如果您只想将粘贴复制到终端中:
/Applications/Python\ 3.6/Install\ Certificates.command

感谢你!

这也可能是因为我最近换了一台新机器,它仍然只安装了部分:)

供将来参考:如果您想使用 Python dmg 安装程序,您还必须阅读 Python 3 的自述文件并运行/Applications/Python 3.6/Install Certificates.command bash 脚本来安装更新的证书。

感谢您的快速回复,@aselle!

谢谢! 有效! :D

谢谢

您能否将 contrib/learn/python/learn/datasets/mnist.py 中的 MNIST 下载 URL 更改为不使用 https ? 这将帮助遇到此问题的人。 在浏览器中尝试过,http 下载链接有效。

要更改的行:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '
新队:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '

我看到两个网址都一样。

您能否将 contrib/learn/python/learn/datasets/mnist.py 中的 MNIST 下载 URL 更改为不使用 https ? 这将帮助遇到此问题的人。 在浏览器中尝试过,http 下载链接有效。

要更改的行:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '
新队:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '

它对我有用。

我们只需要在终端输入
open "/Applications/Python 3.6/Install Certificates.command"

您能否将 contrib/learn/python/learn/datasets/mnist.py 中的 MNIST 下载 URL 更改为不使用 https ? 这将帮助遇到此问题的人。 在浏览器中尝试过,http 下载链接有效。
要更改的行:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '
新队:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '

我看到两个网址都一样。

将“ https://... ”更改为“ http://...

/Applications/Python 3.7/Install Certificates.command
为我工作

如何在 Manjaro Linux 中做到这一点? 我有 Python3.7 张量流。

如果您只想将粘贴复制到终端中:
/Applications/Python\ 3.6/Install\ Certificates.command

谢啦! 这个对我有用!
我只是在 jupyter notebook 中运行代码:
!/Applications/Python\ 3.6/Install\ Certificates.command

在 linux 的情况下...

转到.local/python3.X/lib/python3.6/site-packages/keras/utils/data_utils.py

并在导入语句下面添加这些----

```导入请求
requests.packages.urllib3.disable_warnings()
导入 ssl

尝试:
_create_unverified_https_context = ssl._create_unverified_context
除了属性错误:
# 默认情况下不验证 HTTPS 证书的旧版 Python
经过
别的:
# 处理不支持HTTPS验证的目标环境
ssl._create_default_https_context = _create_unverified_https_context
``

现在尝试新的 python 实例,希望它可以工作:smile_cat:

这也可能是因为我最近换了一台新机器,它仍然只安装了部分:)

供将来参考:如果您想使用 Python dmg 安装程序,您还必须阅读 Python 3 的自述文件并运行/Applications/Python 3.6/Install Certificates.command bash 脚本来安装更新的证书。

感谢您的快速回复,@aselle!

你用这条评论救了我的命! <3

您能否将 contrib/learn/python/learn/datasets/mnist.py 中的 MNIST 下载 URL 更改为不使用 https ? 这将帮助遇到此问题的人。 在浏览器中尝试过,http 下载链接有效。

要更改的行:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '
新队:
SOURCE_URL = ' https://storage.googleapis.com/cvdf-datasets/mnist/ '

这在 ubuntu 上对我有用
通过改变~/miniconda/envs/py36/lib/python3.6/site-packages/tensorflow_core/python/keras/datasets/mnist.py

新队
origin_folder = 'http://storage.googleapis.com/tensorflow/tf-keras-datasets/'

这对我有用:

  1. 手动下载mnist数据集文件(错误信息中给出了url)
  2. 将该文件复制到 ~/.keras/datasets/

就这样。 keras 下载实用程序在通过网络之前在该文件夹中查找缓存数据。

(Python 3.7.3,张量流 2.1.0)

/Applications/Python 3.6/Install Certificates.command 这也适用于虚拟环境吗?

此页面是否有帮助?
0 / 5 - 0 等级