SSL证书错误:在下载Torchvision数据集时,出现在VS Code上。

huangapple go评论127阅读模式
英文:

SSL Certificate Error on VS Code while downloading Torchvision dataset

问题

尝试在VS Code中导入CIFAR10数据集时,运行代码时出现了错误。请查看下面附上的代码和输出。

  1. from torchvision import datasets
  2. data_path = 'dataset_cifar/'
  3. cifar10 = datasets.CIFAR10(data_path, train=True, download=True)
  4. cifar10_val = datasets.CIFAR10(data_path, train=False, download=True)

我收到以下错误信息:

  1. SSLCertVerificationError Traceback (most recent call last)
  2. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
  3. 1345 try:
  4. -> 1346 h.request(req.get_method(), req.selector, req.data, headers,
  5. 1347 encode_chunked=req.has_header('Transfer-encoding'))
  6. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
  7. 1284 """Send a complete request to the server."""
  8. -> 1285 self._send_request(method, url, body, headers, encode_chunked)
  9. 1286
  10. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
  11. 1330 body = _encode(body, 'body')
  12. -> 1331 self.endheaders(body, encode_chunked=encode_chunked)
  13. 1332
  14. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in endheaders(self, message_body, encode_chunked)
  15. 1279 raise CannotSendHeader()
  16. -> 1280 self._send_output(message_body, encode_chunked=encode_chunked)
  17. 1281
  18. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in _send_output(self, message_body, encode_chunked)
  19. 1039 del self._buffer[:]
  20. -> 1040 self.send(msg)
  21. 1041
  22. ...
  23. URLError:

以上是代码和错误信息的翻译。

英文:

I was trying to import the CIFAR10 dataset in VS Code. However, an error pops up when trying to run the code. Please find below the attached code and output.

  1. from torchvision import datasets
  2. data_path= 'dataset_cifar/'
  3. cifar10=datasets.CIFAR10(data_path,train=True,download=True)
  4. cifar10_val=datasets.CIFAR10(data_path,train=False,download=True)

I get the following error.

  1. ---------------------------------------------------------------------------
  2. SSLCertVerificationError Traceback (most recent call last)
  3. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
  4. 1345 try:
  5. -> 1346 h.request(req.get_method(), req.selector, req.data, headers,
  6. 1347 encode_chunked=req.has_header('Transfer-encoding'))
  7. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
  8. 1284 """Send a complete request to the server."""
  9. -> 1285 self._send_request(method, url, body, headers, encode_chunked)
  10. 1286
  11. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
  12. 1330 body = _encode(body, 'body')
  13. -> 1331 self.endheaders(body, encode_chunked=encode_chunked)
  14. 1332
  15. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in endheaders(self, message_body, encode_chunked)
  16. 1279 raise CannotSendHeader()
  17. -> 1280 self._send_output(message_body, encode_chunked=encode_chunked)
  18. 1281
  19. C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\http\client.py in _send_output(self, message_body, encode_chunked)
  20. 1039 del self._buffer[:]
  21. -> 1040 self.send(msg)
  22. ...
  23. -> 1349 raise URLError(err)
  24. 1350 r = h.getresponse()
  25. 1351 except:
  26. URLError:

答案1

得分: 1

将以下内容添加到您的代码中,以“解决”证书问题:

  1. import ssl
  2. ssl._create_default_https_context = ssl._create_unverified_context
英文:

add this to your code to "workaround" the certification like

  1. import ssl
  2. ssl._create_default_https_context = ssl._create_unverified_context

huangapple
  • 本文由 发表于 2023年7月6日 14:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定