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
27.26k stars 12.6k forks source link

Python 3.10 requires some operation when downloading, which is a bit different from the description in the book #586

Open jinxiyu opened 1 year ago

jinxiyu commented 1 year ago
import os
import tarfile
import urllib
import ssl
ssl._create_default_https_context = ssl._create_unverified_context

DOWNLOAD_ROOT ="https://raw.githubusercontent.com/ageron/handson-ml2/master/"
HOUSING_PATH = os.path.join("datasets","housing")
HOUSING_URL= DOWNLOAD_ROOT+"datasets/housing/housing.tgz"

def fetch_housing_data(housing_url=HOUSING_URL,housing_path=HOUSING_PATH):
    os.makedirs(housing_path,exist_ok=True)
    tgz_path=os.path.join(housing_path,"housing.tgz")
    urllib.request.urlretrieve(housing_url,tgz_path)
    housing_tgz=tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()

fetch_housing_data() 

The following is required to Cancel certificate validation globally

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
ageron commented 1 year ago

Hi @jinxiyu ,

Thanks for your feedback. If you are getting an SSL error after installing Python on MacOSX, then it's most likely because the SSL certificates are not installed automatically, you need to install them. This is a common problem, so I added it to the FAQ on the project's home page:

I'm getting an SSL error on MacOSX

You probably need to install the SSL certificates (see this StackOverflow question). If you downloaded Python from the official website, then run /Applications/Python\ 3.8/Install\ Certificates.command in a terminal (change 3.8 to whatever version you installed). If you installed Python using MacPorts, run sudo port install curl-ca-bundle in a terminal.

⚠️⚠️⚠️ Please do not use ssl._create_default_https_context = ssl._create_unverified_context: it is really unsafe! It is basically turning all SSL security off.

Hope this helps.