SelestL Solutions
Technology

How to Fix SSL Certificate Verify Failed in Python

By SelestL • Published on
How to Fix SSL Certificate Verify Failed in Python

You write a simple script to fetch an API, scrape web data with BeautifulSoup, or install packages with pip, and execution crashes immediately with an OpenSSL exception: urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)> or requests.exceptions.SSLError: HTTPSConnectionPool: Max retries exceeded with url (Caused by SSLError(SSLCertVerificationError)). Your network connection is active and the destination website opens fine in your web browser, yet Python refuses to connect.

The SSL certificate verify failed in Python error occurs when Python's internal SSL/TLS implementation cannot validate the target server's public certificate against a trusted Certificate Authority (CA) root store. Unlike web browsers that maintain continuously synchronized commercial root certificate databases, Python relies on environment-specific trust stores that often lack updated root bundles or break behind corporate security proxies.

While disabling SSL verification with verify=False is frequently suggested on online forums, doing so strips all encryption security and leaves your application vulnerable to man-in-the-middle attacks. In this comprehensive technical guide, we explain why Python fails to verify SSL certificates across macOS, Windows, Linux, and enterprise proxy environments, providing permanent, production-grade solutions. If your team requires scalable backend integration or API automation, discover our specialized software engineering and API development services.

1. Why Python Fails to Verify SSL Certificates

1. Why Python Fails to Verify SSL Certificates

When your Python program initiates an HTTPS handshake, the destination web server presents its SSL certificate chain (server certificate, intermediate CA, and root CA). Python's ssl module checks this chain against trusted anchors. Verification fails when one of four common bottlenecks occurs:

  • Missing Default Certificates on macOS: Official Python installers downloaded from Python.org for macOS do not use the native Apple Keychain trust store. Instead, they bundle a private copy of OpenSSL that has no root certificates installed by default.
  • Outdated Certifi Root Bundle: Libraries like requests and urllib3 rely on the Mozilla CA certificate bundle distributed by the Certifi package. If Certifi is outdated, newly rotated intermediate certificates cannot be verified.
  • Corporate Firewalls and Intercepting Proxies: Enterprise networks (Zscaler, Palo Alto Networks, Fortinet) perform SSL inspection by re-signing traffic with internal corporate certificates. If Python does not know your company's custom root CA, verification aborts instantly.
  • System Clock Drift: SSL certificates are strictly bounded by valid date ranges. If your machine's system clock drifts significantly into the past or future, valid certificates are rejected as expired.

Security Warning: Avoid verify=False in Production

Setting verify=False in requests or creating an unverified SSL context with ssl._create_unverified_context() stops all TLS certificate validation. Any attacker on your local network or public Wi-Fi can intercept passwords, tokens, and private data.

2. Step-by-Step Fixes for macOS, Windows, and Linux

Apply these targeted fixes based on your operating system and environment:

Fix 1: Run the macOS Install Certificates Command

If you are on a Mac and installed Python from Python.org, a certificate installation script is included in your Python application folder. Running this command links the Certifi bundle directly to Python's OpenSSL environment:

# For Python 3.12 (adjust version number to match your installation)
/Applications/Python\ 3.12/Install\ Certificates.command

# Or run dynamically via your active Python interpreter
python3 -c "import ssl, certifi; print(certifi.where())"

Once the script executes, it installs updated CA certificates into /Library/Frameworks/Python.framework/Versions/.../etc/openssl/certs, resolving the unable to get local issuer certificate error permanently.

Fix 2: Upgrade the Certifi and Requests Packages

Ensure your Python environment is running the latest Mozilla root bundle by upgrading Certifi, urllib3, and requests:

# Upgrade certificate packages in your virtualenv or global environment
pip install --upgrade certifi requests urllib3

Fix 3: Explicitly Pass the CA Bundle in Python Requests

According to the official Requests documentation, you can point requests directly to Certifi's validated certificate bundle:

import requests
import certifi

# Pass certifi bundle explicitly to guarantee valid root validation
response = requests.get('https://api.example.com/data', verify=certifi.where())
print(response.status_code)

3. Resolving SSL Errors Behind Corporate Proxies and VPNs

In enterprise corporate environments where firewalls inspect HTTPS traffic, Python does not automatically trust self-signed company root certificates. You must instruct Python and pip to trust your organization's custom root CA file:

# Set environment variables for requests and OpenSSL (Linux / macOS)
export REQUESTS_CA_BUNDLE=/path/to/corporate-ca.crt
export SSL_CERT_FILE=/path/to/corporate-ca.crt

# Windows PowerShell
$env:REQUESTS_CA_BUNDLE = "C:\certs\corporate-ca.crt"
$env:SSL_CERT_FILE = "C:\certs\corporate-ca.crt"

Fixing Pip SSL Certificate Failures

If running pip install itself fails with SSL verification errors behind a corporate proxy, configure pip to use your trusted corporate bundle:

# Tell pip to use your organization custom CA bundle
pip config set global.cert /path/to/corporate-ca.crt

4. Diagnostic Matrix: Common SSL Error Scenarios

Error Message Root Cause Operating System Recommended Solution
unable to get local issuer certificate Missing OpenSSL CA roots macOS Run Install Certificates.command
certificate verify failed: self signed certificate Corporate proxy / firewall inspection Windows / Linux Set REQUESTS_CA_BUNDLE to company CRT
certificate has expired Outdated Certifi or system clock drift All Platforms Sync OS time; run pip install -U certifi
hostname doesn't match Server certificate issued for different domain All Platforms Verify target URL and server SAN configuration

For more architectural guides on API integration and network protocols, read our technical breakdown on resolving CORS origin errors in modern applications or check our tutorial on setting up CI/CD with GitHub Actions. For custom backend security audits, connect with our team through our contact page.

5. Where to Go From Here

Fixing Python SSL certificate verification properly protects your data pipelines and user credentials from interception. As you build and deploy production Python services, incorporate these reliable security practices:

  • Never commit verify=False to version control; use environment variables to supply trusted CA certificates in staging and production.
  • Keep the certifi dependency regularly updated in your requirements.txt or pyproject.toml lockfiles.
  • In Docker containers, ensure your base image installs the system certificate bundle (apt-get install -y ca-certificates or apk add --no-cache ca-certificates) before installing Python dependencies.

Frequently Asked Questions

What causes the SSL certificate verify failed error in Python?
This error occurs when Python's internal SSL library cannot establish a trusted cryptographic chain between the target server's certificate and a trusted local Certificate Authority (CA) root bundle.
How do I fix unable to get local issuer certificate on macOS?
Open your Applications folder, locate the Python folder (for example, Python 3.12), and double-click the file named Install Certificates.command. This automatically downloads and configures the Certifi root certificate bundle for Python.
Why is setting verify=False in requests dangerous?
Setting verify=False completely disables TLS certificate validation. While traffic remains encrypted, your application will accept any certificate, allowing malicious actors on your network to perform man-in-the-middle attacks and intercept sensitive data.
How do I make Python trust a corporate proxy or self-signed certificate?
Export the corporate root certificate as a PEM or CRT file, and set the REQUESTS_CA_BUNDLE and SSL_CERT_FILE environment variables to point directly to that file path in your shell or configuration.
Tags: #Python #SSL #Security #Web Development #DevOps #Networking

Related Articles