ageron / handson-ml2

A series of Jupyter notebooks that walk you through the fundamentals of Machine Learning and Deep Learning in Python using Scikit-Learn, Keras and TensorFlow 2.
Apache License 2.0
28.06k stars 12.81k forks source link

DOWNLOAD_ROOT (chapter 2 project) #303

Closed yogeshriyat closed 3 years ago

yogeshriyat commented 4 years ago

When I use this line of code for the fetch_housing_data function:

DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/"

I get the following error:

URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)>

I also tried this path which generated the same error: https://raw.githubusercontent.com/ageron/handson-ml2/master/datasets/housing/housing.tgz

Any ideas?

Sorry if this was answered before. I tried doing a search, but couldn't find an answer to this issue.

yogeshriyat commented 4 years ago

This is the entire traceback

`--------------------------------------------------------------------------- SSLCertVerificationError Traceback (most recent call last) /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in do_open(self, http_class, req, **http_conn_args) 1349 try: -> 1350 h.request(req.get_method(), req.selector, req.data, headers, 1351 encode_chunked=req.has_header('Transfer-encoding'))

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py in request(self, method, url, body, headers, encode_chunked) 1254 """Send a complete request to the server.""" -> 1255 self._send_request(method, url, body, headers, encode_chunked) 1256

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py in _send_request(self, method, url, body, headers, encode_chunked) 1300 body = _encode(body, 'body') -> 1301 self.endheaders(body, encode_chunked=encode_chunked) 1302

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py in endheaders(self, message_body, encode_chunked) 1249 raise CannotSendHeader() -> 1250 self._send_output(message_body, encode_chunked=encode_chunked) 1251

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py in _send_output(self, message_body, encode_chunked) 1009 del self._buffer[:] -> 1010 self.send(msg) 1011

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py in send(self, data) 949 if self.auto_open: --> 950 self.connect() 951 else:

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py in connect(self) 1423 -> 1424 self.sock = self._context.wrap_socket(self.sock, 1425 server_hostname=server_hostname)

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py in wrap_socket(self, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, session) 499 # ctx._wrap_socket() --> 500 return self.sslsocket_class._create( 501 sock=sock,

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py in _create(cls, sock, server_side, do_handshake_on_connect, suppress_ragged_eofs, server_hostname, context, session) 1039 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets") -> 1040 self.do_handshake() 1041 except (OSError, ValueError):

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py in do_handshake(self, block) 1308 self.settimeout(None) -> 1309 self._sslobj.do_handshake() 1310 finally:

SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

During handling of the above exception, another exception occurred:

URLError Traceback (most recent call last)

in ----> 1 fetch_housing_data() in fetch_housing_data(housing_url, housing_path) 7 os.makedirs(housing_path) 8 tgz_path = os.path.join(housing_path, "housing.tgz") ----> 9 urllib.request.urlretrieve(housing_url, tgz_path) 10 housing_tgz = tarfile.open(tgz_path) 11 housing_tgz.extractall(path=housing_path) /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in urlretrieve(url, filename, reporthook, data) 245 url_type, path = _splittype(url) 246 --> 247 with contextlib.closing(urlopen(url, data)) as fp: 248 headers = fp.info() 249 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context) 220 else: 221 opener = _opener --> 222 return opener.open(url, data, timeout) 223 224 def install_opener(opener): /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in open(self, fullurl, data, timeout) 523 524 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method()) --> 525 response = self._open(req, data) 526 527 # post-process response /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in _open(self, req, data) 540 541 protocol = req.type --> 542 result = self._call_chain(self.handle_open, protocol, protocol + 543 '_open', req) 544 if result: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args) 500 for handler in handlers: 501 func = getattr(handler, meth_name) --> 502 result = func(*args) 503 if result is not None: 504 return result /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in https_open(self, req) 1391 1392 def https_open(self, req): -> 1393 return self.do_open(http.client.HTTPSConnection, req, 1394 context=self._context, check_hostname=self._check_hostname) 1395 /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py in do_open(self, http_class, req, **http_conn_args) 1351 encode_chunked=req.has_header('Transfer-encoding')) 1352 except OSError as err: # timeout error -> 1353 raise URLError(err) 1354 r = h.getresponse() 1355 except: URLError: `
ibeauregard commented 4 years ago

This might help : https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for-http-en-wikipedia-org.

ageron commented 4 years ago

Thanks for your question.

Generally, when people have SSL issues when using Python on MacOSX, it's due to a missing step when installing Python: installing the SSL certificates. You need to run this script (change the Python version to match the version you installed) :

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

Hope this helps.

ageron commented 3 years ago

Note: if you installed Python using MacPorts, open a terminal and run sudo port install curl-ca-bundle.

Since this is quite a frequent question, so I added it to the new FAQ section on the project's home page.

I'm closing this issue, but please reopen if the problem persists after installing the SSL certificates.