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

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

SSL Certificate Error on VS Code while downloading Torchvision dataset

问题

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

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

我收到以下错误信息:

SSLCertVerificationError                  Traceback (most recent call last)
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)
   1345             try:
-> 1346                 h.request(req.get_method(), req.selector, req.data, headers,
   1347                           encode_chunked=req.has_header('Transfer-encoding'))

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)
   1284         """Send a complete request to the server."""
-> 1285         self._send_request(method, url, body, headers, encode_chunked)
   1286 

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)
   1330             body = _encode(body, 'body')
-> 1331         self.endheaders(body, encode_chunked=encode_chunked)
   1332 

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)
   1279             raise CannotSendHeader()
-> 1280         self._send_output(message_body, encode_chunked=encode_chunked)
   1281 

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)
   1039         del self._buffer[:]
-> 1040         self.send(msg)
   1041 

...

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.

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

I get the following error.

    ---------------------------------------------------------------------------
SSLCertVerificationError                  Traceback (most recent call last)
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)
   1345             try:
-> 1346                 h.request(req.get_method(), req.selector, req.data, headers,
   1347                           encode_chunked=req.has_header('Transfer-encoding'))

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)
   1284         """Send a complete request to the server."""
-> 1285         self._send_request(method, url, body, headers, encode_chunked)
   1286 

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)
   1330             body = _encode(body, 'body')
-> 1331         self.endheaders(body, encode_chunked=encode_chunked)
   1332 

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)
   1279             raise CannotSendHeader()
-> 1280         self._send_output(message_body, encode_chunked=encode_chunked)
   1281 

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)
   1039         del self._buffer[:]
-> 1040         self.send(msg)
...
-> 1349                 raise URLError(err)
   1350             r = h.getresponse()
   1351         except:

URLError: 

答案1

得分: 1

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

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

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

import ssl
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:

确定