DevOps | Cloud | Analytics | Open Source | Programming





How To Fix - "Ssl: Certificate_Verify_Failed” Error in Python ?



In this post , we will see How To Fix - "Ssl: Certificate_Verify_Failed” Error” Error in Python . Sometimes while trying to access a specific web-page through a python script , it generates the following error


URLError: <urlopen error \[SSL: CERTIFICATE\_VERIFY\_FAILED\] certificate verify failed

Let us see the various options we have to fix the above issue.  

  • Generate fresh certificates and make an environment variable to point to the certificates directory

$ sudo update-ca-certificates --fresh
$ export SSL\_CERT\_DIR=/etc/ssl/certs

 

  • Verify if your openSSL version is outdated. Sometimes the simplest solution could be just to update\urgrade to the latest recent version of "certifi" Package. Also sometimes , your system might be missing the "certify" package of certificates altogether.The details about these are present in the "ReadMe.rtf" guide in your Python installed location.
So it might also could be a matter of running the post-install script to install the certificates - "Certificates.command".


./Install Certificates.command

If you are unable to find the "certifi", then use below set of commands to proceed ahead with the SSL error issue fix -


pip install certifi
/YOUR\_PYTHON\_INSTALLED\_LOCATION/Python/3.6/Install/ Certificates.command

 

  • The second option would be a Dirty approach of bypassing the certificate verification (Not a Good approach through) . If you are just trying out anything quick-&-test kind of thing , it is okay . But if you are Building a Software or an Actual solution , this approach is not a recommended one.
This creates an unverified context. If you are interested to dig deeper , more details about SSL Context could be found here - SSLContext  


import ssl
context = ssl.\_create\_unverified\_context()
urllib.urlopen("https://URL\_LINK", context=context)

or 

import ssl
ssl.\_create\_default\_https\_context = ssl.\_create\_unverified\_context

  Hope this solves the issue.   Additional Read -