HOWTO · Python

How to Fix the SSL: CERTIFICATE_VERIFY_FAILED Error in Python

Diagnose the causes of Python’s `SSL: CERTIFICATE_VERIFY_FAILED` error and safely restore TLS certificate verification.

On this page

SSL: CERTIFICATE_VERIFY_FAILED means Python could not establish that the certificate presented for a TLS connection chains to a certificate authority it trusts and matches the requested host. This is a client-side verification failure; it does not mean that a certificate authority returned an error because a domain does not exist.

TLS encrypts traffic and authenticates the server through its certificate. Verification should remain enabled while you identify which part of that trust check failed.

What SSL: CERTIFICATE_VERIFY_FAILED Means

A TLS certificate is presented by the server during the connection. The client checks its validity period, hostname, issuer chain, and whether that chain leads to a trusted root certificate in the client’s trust store. Python’s ssl module performs these checks when a verifying SSL context is used. Requests also verifies certificates by default.

The error can arise from a problem on either side of the connection. For example, the server may omit an intermediate certificate, while a local Python environment may have an outdated CA bundle. A hostname mismatch, incorrect system clock, private certificate authority, or TLS-inspecting proxy can also explain the failure. The error alone does not identify which cause applies.

Diagnose the Cause

Use the same hostname and Python environment as the failing application while checking these items:

  1. Check the server certificate and chain. Confirm the certificate is in date, is issued for the hostname in the URL, and includes the intermediate certificates needed to reach its issuer. If a browser or another client works, compare its trust configuration and network path; that difference is a clue, not proof that the server is correct.
  2. Check the clock. Make sure the machine or container has the correct date and time. A clock far outside a certificate’s validity period can make a valid certificate appear expired or not yet valid.
  3. Identify the Python environment and HTTP client. Check which interpreter runs the application and whether the failing request uses the standard library, Requests, or another package. A system Python, virtual environment, container, and packaged application may each use a different trust store.
  4. Check the trust source and network path. Determine whether the server uses a private CA and whether a corporate proxy intercepts TLS. Ask the network or security administrator for the approved CA certificate and instructions for that environment. Do not download a certificate from an unverified source just to make the error disappear.

If the server certificate is expired, has the wrong hostname, or lacks required intermediates, ask the server owner to repair the deployment. Changing the client’s CA bundle cannot correct a broken server chain.

Fix the Certificate or Trust Store

For a server-side problem, the durable fix is for the server owner to renew or correctly configure the certificate chain for the requested hostname. Retrying with a different browser may help compare behavior, but it does not repair the certificate or make an untrusted connection safe.

For a private service or approved TLS-inspecting proxy, obtain the organization’s root CA certificate through its trusted distribution channel. Install it in the trust store used by the relevant operating system, container, or Python environment, or configure the application to use that CA bundle. Follow the administrator’s documented process so that the certificate’s origin and intended scope are known.

A stale CA bundle can also cause failures when a server uses a newer, otherwise valid public CA. Update the trust store or CA-bundle package used by the application. Updating pip may help pip itself resolve package-download certificates in some Python installations; updating certifi may refresh the bundle Requests uses when configured to use it. These updates do not fix an expired server certificate, a hostname mismatch, a missing server intermediate, or an organization-specific CA that has not been trusted.

Configure Python and Requests to Verify Certificates

Python’s standard-library ssl module and Requests can use different trust sources. ssl.create_default_context() loads the defaults available to that Python/OpenSSL environment, which can depend on its build and platform configuration. Requests normally verifies with the CA bundle supplied by certifi; session.verify = True means “use Requests’ configured default bundle,” not “use Python’s ssl defaults” or necessarily the operating system store. Requests can also take a bundle path from REQUESTS_CA_BUNDLE (or CURL_CA_BUNDLE) unless you configure the session explicitly.

This short example shows that Python’s default context requires certificate verification and is the self-contained success case:

import ssl

context = ssl.create_default_context()
print(context.verify_mode == ssl.CERT_REQUIRED)
True

For a private CA, give Python the trusted CA bundle as cafile, then pass that verifying context to the standard-library HTTPS request. Replace both values with the service URL and bundle path supplied by your administrator. This service-backed example is unexecuted here; it catches connection errors so readers can run it after supplying their values.

import ssl
import urllib.error
import urllib.request

SERVICE_URL = "https://service.example"
CA_BUNDLE = "/path/to/organization-ca-bundle.pem"

try:
    context = ssl.create_default_context(cafile=CA_BUNDLE)
    with urllib.request.urlopen(SERVICE_URL, context=context, timeout=10) as response:
        print(response.status)
except (OSError, urllib.error.URLError) as error:
    print(f"Connection not completed: {type(error).__name__}: {error}")

Requests’ verify setting accepts a CA bundle path. Assign the organization-provided bundle to the session and use that same session for the request; certificate checking remains enabled. Replace the URL and path with values for your service. This service-backed example is also unexecuted here.

import requests

SERVICE_URL = "https://service.example"
CA_BUNDLE = "/path/to/organization-ca-bundle.pem"

session = requests.Session()
session.verify = CA_BUNDLE

try:
    response = session.get(SERVICE_URL, timeout=10)
    print(response.status_code)
except (OSError, requests.RequestException) as error:
    print(f"Connection not completed: {type(error).__name__}: {error}")

Use a CA bundle whose issuer you verified through your organization’s trusted channel. A custom bundle must contain the CA certificates needed to validate the service’s chain; configuring a CA does not repair an expired certificate, hostname mismatch, or missing intermediate on the server. If Requests should use its normal bundle, leave session.verify at its default instead of assigning True; use an explicit path for an organization CA. Updating certifi can refresh Requests’ bundled public roots when that is the stale trust source, while updating pip may help pip’s own package downloads. Neither update changes Python’s ssl trust configuration in every environment or fixes a broken server certificate.

Why You Should Not Disable Certificate Verification

Disabling certificate verification allows an attacker or an untrusted proxy to present a certificate for the wrong identity and still have the client accept the connection. Encryption alone does not prove that the peer is the intended server. Credentials, tokens, and response data could then be exposed or altered.

Do not use verify=False, an unverified SSL context, or a global replacement for Python’s default HTTPS context as a repair. Those options hide the diagnostic signal and weaken every affected connection. Restore verification by fixing the server chain, the client’s clock or trust store, or the explicitly configured trusted CA bundle.