Sling Academy
Home/Python/Fixing Python Requests SSLError: CERTIFICATE_VERIFY_FAILED

Fixing Python Requests SSLError: CERTIFICATE_VERIFY_FAILED

Last updated: January 02, 2024

The SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] is a common error encountered when using Python’s Requests library. This error occurs when the SSL certificate verification fails. Below are several solutions that can help resolve this error, each with its pros and cons.

Solution 1: Update Certificates

Update your system’s certificate bundle to ensure Python can validate SSL certificates properly.

  1. Locate Python’s certificate file, typically found in your Python installation’s directory.
  2. Update the certificate bundle using your system’s package manager or by downloading the latest file from a trusted certificate authority.
  3. Restart your Python application.

Example: Not applicable – This is a system-level solution without specific Python code.

Pros: Updates the system’s trust store; fixes the root cause.
Cons: Might require administrative access; not a quick in-code fix.

Solution 2: Use certifi Package

Utilize the Python ‘certifi’ package which provides Mozilla’s CA Bundle.

  1. Install the ‘certifi’ package using pip install certifi.
  2. Include the ‘certifi’ certificate bundle in your requests.

Code Example:

import requests
import certifi

response = requests.get('https://example.com', verify=certifi.where())
print(response.text)

Pros: Easy to implement; no system-wide changes.
Cons: Adds dependency; might not solve all SSL issues.

Solution 3: Manually Specify Certificate Path

Manually provide the path to a trusted certificate bundle when making requests.

  1. Obtain a path to a trusted certificate file or bundle.
  2. Pass that path to the verify argument in your request.

Code Example:

import requests

trusted_cert = '/path/to/truststore.pem'
response = requests.get('https://example.com', verify=trusted_cert)
print(response.text)

Pros: Precise control over certificates; good for custom setups.
Cons: Requires manual management; error-prone.

Solution 4: Disable SSL Verification (Not Recommended)

Turn off SSL certificate verification (use with extreme caution).

  1. Add a verify=False parameter to your request.

Code Example:

import requests

warning import warnings # 1.
from requests.packages.urllib3.exceptions import InsecureRequestWarning # 2.

warnings.simplefilter('ignore', InsecureRequestWarning) # 3.

response = requests.get('https://example.com', verify=False) # 4.
print(response.text)

Note: This example also includes suppressing insecure request warnings to prevent cluttering the console output.

Pros: Quick workaround; good for troubleshooting.
Cons: Extremely insecure; exposes to man-in-the-middle attacks.

Next Article: Python Requests module: How to add API key/credentials to HTTP request

Previous Article: Python requests module: How to download a large file smoothly

Series: Python: Network & JSON tutorials

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types