Solving SSL Python Problem – SSL: CERTIFICATE_VERIFY_FAILED with Python3

SSL: CERTIFICATE_VERIFY_FAILED with Python3 is an error which occurs when there is no ssl certificate installed.

This post is a guide showing you why you are having this error and how you can get rid of it in the most efficient way possible, I will also include some alternative solutions that may help you.

Explaining the Error SSL: CERTIFICATE_VERIFY_FAILED with Python3

This is an error which occurs when there is no ssl certificate installed on your machine.

The error message should look like the error in the example bellow, make sure you have the same error message in order to avoid confusion.

                                                                       #
    h.request(req.get_method(), req.selector, req.data, headers)
....
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/ssl.py", line 628, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully …

Solution 1 : Install the ssl certificate

The first solution is simple, try to navigate to the folder where Python is installed

Inside the folder, you should look for a file called Install Certificates.command.

Double-click the Install Certificates.command and the certificate will be installed. and as a result the error should be gone.

You can alternatively use the command bellow.

                                                                       #
pip install --upgrade certifi
                                                                       #

This solution should be enough to solve the issue and the error should disappear. If that is not the case, please consider trying the solution bellow.

Solution 2 : use the ssl module.

If the solution above does not do the job you can try to use the ssl module, you do no have to use the install Certificates.command, you can try this workaround instead. Just like in the example bellow.

After that urllib.request.urlopen(urllink) can be used to read the link content.

                                                                       #
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
                                                                       #

This solution should have solved the issue and the error should be gone. If that is not the case, please consider trying the solution bellow.

Solution 3 : use update-ca-certificates.

The third solution is for Debian users, you can try the command bellow

                                                                       #
sudo update-ca-certificates --fresh
                                                                       #

And finally run the command

                                                                       #
export SSL_CERT_DIR=/etc/ssl/certs
                                                                       #

I hope this guide solved your problem, thank you for reading.

Summing-up

This is the end of this article guys, I hope one of these solutions worked for you depending on which OS you have, I wish you good luck with your Python Journey.

For donations you can use the red Kofi button above, keep learning and keep coding, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/