Categories:

Python3 on Mac SSL certificate verify failed

Lib: json, urllib.request

Sample code

import json
from urllib.request import urlopen

url = "https://api.github.com/"

def get(url, object_hook=None):
    with urlopen(url) as resource:
        return json.load(resource, object_hook=object_hook)

data = get(url)
print(data)

Error

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

Solution via Install Certificate.command

Solution via pip upgrade

pip install --upgrade certifi

Solution workaround in code

# Use undefinded SSL

import ssl
ssl._create_default_https_context = ssl._create_unverified_context