How to fix the error: “Certificate verify failed: unable to get local issuer certificate” in Python?

Python is not as complex as it seems. If you know the language, you can easily design applications and work on any project that you want to program. Beginners are learning this language as programming is incomplete without Python. When you are working on Python, it’s quite normal to have errors. The ‘error:“Certificate verify failed: unable to get local issuer certificate” in Python is one of those exceptions that your program throws.

Today, we are going to discuss how you get this error as well as the ways to fix it. Go through the article till the end to get the solution to the error warning you are here for

How do you get an error warning?

The error can show up when urlopen and BeautifulSoup are used. You get a warning ‘error:“Certificate verify failed: unable to get local issuer certificate” in Python’.  Check out how you get the error

raise URLError(err) 
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1049)>

This is how you get the exception at the time of coding.

How to handle the error:“Certificate verify failed: unable to get local issuer certificate” in Python’?

Install Pip

The simplest way to resolve the error is to install certificates using the pip command. This is how you can do this:

pip install certifi

Although the code seems really seems small, it is powerful enough to solve the issue.

Update the certificate using pip

Another easiest solution is to update the certificate, and you need to do this using pip. Have a look at the command

pip install --upgrade certify

This update can fix the exception you are getting.

Try unverified SSL

You can always use an unverified SSL if you don’t need the verified one. To solve the error, you need to insert two lines in the code. Implement the below code

import ssl 
ssl._create_default_https_context = ssl._create_unverified_context

This solution is effective to tackle the error warning that pops up.

Use cacert.pem to add certificates

  • You need to look for the path where your cacert-pem is located
  • To get the full path, you need to use certify.where()
  • Once done, use a browser to open the URL. The chain of certificates should be downloaded and saved with the name Base64 encoded .cer.
  • Use notepad to open the cacert.pem. Now you can just need to add (—Begin Certificate— *** —End Certificate—) at the end of every certificate’s content.

This makes your program run without any error.

Command for all operating systems

No matter which operating system you are using for python programming, you can get the error fixed. Have a look at the code

import ssl
import certifi
from urllib.request import urlopen

request = "https://example.com"
urlopen(request, context=ssl.create_default_context(cafile=certifi.where()))

Conclusion

The issue “Certificate verify failed: unable to get local issuer certificate” in Python has been discussed. Several ways are highlighted, go ahead with the way you want. Hope it addressed your issue!

Reference Source:

https://ittutoria.net/certificate-verify-failed-unable-to-get-local-issuer-certificate-in-python/

https://stackoverflow.com/questions/52805115/certificate-verify-failed-unable-to-get-local-issuer-certificate

Leave a Reply

Your email address will not be published. Required fields are marked *