How to Fix Windows Python SSL Certificate Verify Failed in Anaconda/Jupyter: Resolving Requests & Google Analytics API Errors with Certifi
If you’ve encountered the dreaded SSL certificate verify failed error while working with Python in Anaconda or Jupyter Notebook on Windows—especially when using libraries like requests or interacting with APIs like Google Analytics—you’re not alone. This error occurs when Python cannot validate the SSL certificate of the server it’s trying to connect to, often due to missing or outdated Certificate Authority (CA) certificates.
In this guide, we’ll demystify the root causes of this error and walk through actionable solutions using certifi, a Python package that provides trusted CA certificates. By the end, you’ll be able to resolve SSL issues for requests and the Google Analytics API, ensuring secure and uninterrupted connections in your Anaconda/Jupyter environment.
Understanding the "SSL Certificate Verify Failed" Error#
SSL (Secure Sockets Layer) certificates are used to encrypt data between your Python code and a server (e.g., https://api.google-analytics.com). To ensure security, Python checks if the server’s certificate is signed by a trusted CA. If Python cannot find the necessary CA certificates locally, it throws the SSL certificate verify failed error.
In Anaconda/Jupyter environments on Windows, this error typically stems from:
Outdated or missing CA certificates in the environment.
Python libraries (like requests or Google Analytics client) not pointing to the correct CA certificate bundle.
Outdated certifi Package: certifi is a Python library that bundles trusted CA certificates. If it’s outdated, it may lack newer CA roots.
Incorrect Certificate Path: Python/Anaconda may be using an old or empty CA certificate file (e.g., cacert.pem).
Environment Variables Misconfiguration: Libraries like requests rely on environment variables (e.g., REQUESTS_CA_BUNDLE) to locate CA certificates. These may be unset or pointing to the wrong path.
Proxy/Firewall Interference: Corporate proxies or firewalls sometimes block or alter SSL certificates, causing validation failures.
By default, requests uses the system’s CA certificates, which may be outdated on Windows. Force it to use certifi’s bundle with the verify parameter:
import requests import certifi url = "https://www.google.com" # Test with a secure site response = requests.get(url, verify=certifi.where()) # Explicitly use certifi print("Status code:", response.status_code) # Should return 200 if successful
The Google Analytics API (e.g., google-analytics-data v1beta) uses underlying HTTP clients that may need explicit CA certificate paths. Use Python’s ssl module to create a secure context with certifi’s bundle:
from google.analytics.data_v1beta import BetaAnalyticsDataClient from google.analytics.data_v1beta.types import RunReportRequest import ssl import certifi # Create an SSL context using certifi's certificates ssl_context = ssl.create_default_context(cafile=certifi.where()) # Initialize the Google Analytics client with the custom SSL context client = BetaAnalyticsDataClient(ssl_context=ssl_context) # Example: Run a report (replace with your property ID) request = RunReportRequest( property=f"properties/123456789", # Replace with your GA4 property ID dimensions=[{"name": "date"}], metrics=[{"name": "activeUsers"}], ) try: response = client.run_report(request) print("Report data:", response) except Exception as e: print("Error:", e)
Libraries like requests respect the REQUESTS_CA_BUNDLE environment variable to locate CA certificates. Set this variable to point to certifi’s cacert.pem to globally fix SSL issues in your Conda environment.
Step 1: Set the Variable Temporarily (Anaconda Prompt Session)#
In the Anaconda Prompt, run:
set REQUESTS_CA_BUNDLE=C:\Users\YourUsername\Anaconda3\envs\your_environment_name\Lib\site-packages\certifi\cacert.pem
Replace the path with the certifi.where() output from Method 1.
Step 2: Set the Variable Permanently (System-Wide)#
To avoid setting the variable every time, add it to Windows System Properties:
Open System Properties: Press Win + Pause/Break → Click "Advanced system settings".
Go to the Advanced tab → Click "Environment Variables".
Under "User variables" (for your user) or "System variables" (all users), click "New".
Variable name: REQUESTS_CA_BUNDLE
Variable value: Paste the certifi.where() path (e.g., C:\Users\YourUsername\Anaconda3\Lib\site-packages\certifi\cacert.pem).
Click "OK" to save. Restart Anaconda/Jupyter for changes to take effect.
Method 4: Replace Anaconda’s Default CA Certificates#
Anaconda uses its own CA certificate bundle at Anaconda3\Library\ssl\cacert.pem. If this file is outdated, replace it with certifi’s bundle.
Locate Anaconda’s CA File: The default path is: C:\Users\YourUsername\Anaconda3\Library\ssl\cacert.pem
(Adjust for custom environments: ...\envs\your_environment_name\Library\ssl\cacert.pem).
Backup Anaconda’s CA File: Rename the existing cacert.pem to cacert_old.pem (in case of issues).
Copy Certifi’s CA File: Copy certifi’s cacert.pem (from certifi.where()) to Anaconda’s ssl folder. For example:
SSL certificate errors in Anaconda/Jupyter on Windows are typically caused by outdated or misconfigured CA certificates. By updating certifi, explicitly referencing its certificates in code, or configuring environment variables, you can resolve these issues for requests and the Google Analytics API. Start with Method 1 (updating certifi), then progress to explicit code configuration or environment variables if needed.