You’re making an HTTP request in Python and suddenly:
Or with requests:
This error means Python can’t verify the SSL certificate of the server you’re connecting to. Here’s why it happens and how to actually fix it (not just disable verification).
Why This Happens
Python uses a certificate bundle to verify SSL certificates. The error occurs when:
- Your system’s CA certificates are outdated or missing
- You’re on macOS and haven’t installed certificates (common after Python install)
- You’re in a corporate environment with a proxy/firewall that intercepts HTTPS
- The server has a self-signed or invalid certificate
- You’re in a Docker container without CA certificates installed
The Wrong Fix (Don’t Do This in Production)
You’ll see this everywhere online:
| |
Or:
| |
This disables SSL verification entirely, making you vulnerable to man-in-the-middle attacks. Only use this for local development with self-signed certs.
The Right Fixes
Fix 1: Update CA Certificates (Linux)
Most common on minimal Docker images or fresh Linux installs:
| |
Then update the certificate store:
| |
Fix 2: Install certifi (Python Package)
The certifi package provides Mozilla’s CA bundle:
| |
Then use it explicitly:
| |
For urllib:
| |
Fix 3: macOS Python Certificate Fix
If you installed Python via python.org installer on macOS, run:
| |
Or manually:
| |
This installs certifi and symlinks it to the system certificate store.
Fix 4: Corporate Proxy / Custom CA
If your company uses a proxy that re-signs HTTPS traffic, you need to add their CA certificate:
| |
Or in Python:
| |
Fix 5: Docker Container Fix
Add this to your Dockerfile:
| |
For Alpine-based images:
| |
Debugging SSL Issues
Check what certificate store Python is using:
| |
Verify a specific certificate chain:
| |
Check if certifi is installed and where:
| |
Quick Reference
| Situation | Fix |
|---|---|
| Fresh Linux/Docker | apt-get install ca-certificates |
| macOS Python install | Run Install Certificates.command |
| Corporate proxy | Add company CA to system store |
| Need portable solution | Use certifi package |
| Self-signed cert (dev only) | verify=False or add cert to bundle |
Summary
The CERTIFICATE_VERIFY_FAILED error almost always means your Python environment is missing CA certificates. Install ca-certificates at the system level, use the certifi package, or add your corporate CA to the trust store. Avoid disabling verification in production—it’s a security risk that will come back to bite you.