Python 3.10 requires some operation when downloading, which is a bit different from the description in the book
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
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.commandin a terminal (change3.8to whatever version you installed). If you installed Python using MacPorts, runsudo port install curl-ca-bundlein 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.