Python rejects a self-signed certificate in the chain
SSL: CERTIFICATE_VERIFY_FAILEDShort answer
Python could not build a trusted path from the server certificate to a root it knows. A company proxy, a private CA or a self-signed server certificate is in the chain. Give Python the CA file that signed it, through verify=, REQUESTS_CA_BUNDLE or an SSL context. Do not set verify=False outside local tests.
Fix it fast
1Find out which cause you have
Print which CA files requests and the ssl module trust right now. The extra CA you need is missing from these.
python -c "from requests.utils import DEFAULT_CA_BUNDLE_PATH; print(DEFAULT_CA_BUNDLE_PATH)" python -c "import ssl; print(ssl.get_default_verify_paths())"2Match what you see, then jump to its fix
- 1
The failing call uses requests, and a browser on the same network trusts the siterequests does not know your private or proxy CAFix - 2
The variable is exported and plain requests.get works, but code using Session.send or PreparedRequest still failsREQUESTS_CA_BUNDLE is set but ignored by a prepared requestFix - 3
No requests in the traceback; the error comes from urllib, http.client or a client library that takes an SSL contextCode that uses the ssl module or urllib needs its own contextFix
- 1
What does the error look like?
SSL: CERTIFICATE_VERIFY_FAILEDcertificate verify failed: self signed certificate in certificate chain (_ssl.c:992)
Cause 1 of 3
requests does not know your private or proxy CA
1Check
requests verifies against the certifi bundle, not the operating system store. A TLS-inspecting proxy or private CA is missing from it.
python -c "import certifi; print(certifi.where())"2Fix
Get the root CA certificate as a PEM file from whoever runs the proxy or server, and pass its path. verify takes a path to a CA bundle; setting REQUESTS_CA_BUNDLE does the same for the whole process.
import requests
r = requests.get("https://api.example.com", verify="/etc/ssl/private-ca/root.pem")
# or for the whole process
# shell: export REQUESTS_CA_BUNDLE=/etc/ssl/private-ca/root.pem3Verify
The same request now returns a response.
python -c "import requests; print(requests.get('https://api.example.com').status_code)"Confirmed by requests.readthedocs.io
Cause 2 of 3
REQUESTS_CA_BUNDLE is set but ignored by a prepared request
1Check
The requests docs say the prepared-request flow does not read environment settings, so a self-signed certificate named in REQUESTS_CA_BUNDLE is not taken into account.
grep -rn "prepare_request\|Request(" --include=*.py .2Fix
Merge the environment settings into the session before sending.
s = requests.Session()
prepped = s.prepare_request(requests.Request("GET", url))
settings = s.merge_environment_settings(prepped.url, {}, None, None, None)
resp = s.send(prepped, **settings)3Verify
The request returns instead of raising SSLError.
python your_script.pyConfirmed by requests.readthedocs.io
Cause 3 of 3
Code that uses the ssl module or urllib needs its own context
1Check
ssl.create_default_context() trusts the system defaults. Look at where those point.
python -c "import ssl; print(ssl.get_default_verify_paths())"2Fix
Build a context that loads your CA file and pass it to the call. Verification stays on, with CERT_REQUIRED.
import ssl, urllib.request
ctx = ssl.create_default_context(cafile="/etc/ssl/private-ca/root.pem")
urllib.request.urlopen("https://api.example.com", context=ctx)3Verify
The call returns a response.
python -c "import ssl; print(ssl.create_default_context(cafile='/etc/ssl/private-ca/root.pem').verify_mode)"Confirmed by docs.python.org
How do I stop it coming back?
- Keep the private CA as a file in your repo's deployment config, not as verify=False in code.
- Update certifi regularly, as the requests docs recommend.
- Use verify=False only in a throwaway local test; it makes the connection open to man-in-the-middle attacks.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
npm rejects the certificate it gets from a company proxy
npm ERR! request to https://registry.npmjs.org/express failed, reason: self signed certificate in certificate chain
A proxy that inspects HTTPS traffic re-signs it with the company's own root certificate, which Node does not know, so npm refuses the connection.
3 causes, each with a fixSecurity
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
On EC2 this error almost always means the instance did not accept your key for that user.
3 causes, each with a fixSecurity
gcloud init cannot finish the sign-in step
Enter authorization code
gcloud init signs in through a browser.
3 causes, each with a fixSecurity
A GitHub Actions SSH deploy step cannot log in to the server
ssh: handshake failed: ssh: unable to authenticate
The runner reached your server but the server refused the login.
4 causes, each with a fixSecurity
SSH to GitHub asks to confirm the host, or hangs on that prompt
ED25519 key fingerprint is SHA256:
SSH has never seen github.com from this machine, so it asks you to trust the server key.
2 causes, each with a fix