pip-system-certs
pip-system-certs automatically configures Python to use system certificates via the `truststore` library. It primarily achieves this by monkey-patching `urllib3`, allowing `pip` and other applications utilizing `urllib3` (like `requests`) to respect OS-provided certificate bundles. It is currently at version 5.3 and typically sees several releases per year, often in conjunction with updates to `pip` or its underlying `truststore` dependency.
Warnings
- gotcha pip-system-certs will not override existing certificate configurations set via `PIP_CERT` environment variable or the `cert` option in `pip.conf`. It's intended for environments not already using a custom certificate bundle.
- breaking Requires Python 3.10 or higher. The underlying `truststore` library leverages features specific to modern Python versions, making `pip-system-certs` incompatible with older Python runtimes.
- gotcha The library works by monkey-patching `urllib3`. This approach can potentially conflict with other libraries or custom code that also modifies `urllib3`'s internal behavior.
Install
-
pip install pip-system-certs
Imports
- pip_system_certs
import pip_system_certs
Quickstart
# IMPORTANT: Before running this code, install pip-system-certs and requests:
# python -m pip install pip-system-certs requests
import requests
import sys
print(f"Python version: {sys.version.splitlines()[0]}")
print("Attempting to fetch https://pypi.org to verify system certificate usage...")
try:
# requests uses urllib3, which pip-system-certs patches to use system certificates.
response = requests.get("https://pypi.org", timeout=10)
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
print(f"Successfully connected to pypi.org! Status: {response.status_code}")
print("This indicates that pip-system-certs (via truststore) is likely active, enabling requests to use your system's trusted certificates.")
except requests.exceptions.SSLError as e:
print(f"SSL Error encountered: {e}")
print("This suggests pip-system-certs might not be correctly configured, system certificates are invalid, or it's not active.")
except requests.exceptions.RequestException as e:
print(f"A general request error occurred: {e}")
print("Please check your network connection and confirm pip-system-certs is installed.")