112

Many operations in Python require accessing things via https. This includes pip install command, or just using http.client.HTTPSConnection, or any modules or applications that use these things internally.

If python was installed from the official python pkg installer, downloaded from https://python.org, then it uses an internal version of openssl, and contains no root certificates. Anything that uses an SSL connection results in this error:

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

How can I install root certs to make the above error go away?

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Edward Ned Harvey
  • 6,525
  • 5
  • 36
  • 45

17 Answers17

241

When you run the python installer, they display this information to you. It is also documented in /Applications/Python 3.6/ReadMe.rtf, but it's very easily overlooked.

Just browse to Applications/Python 3.6 and double-click Install Certificates.command

There is an issue in the Python bug tracker about this. http://bugs.python.org/issue29480

Update: This issue is marked as resolved in the bug tracker with this text being part of the latest comment:

... For 3.7.0b2, I have tried to make things more obvious in two ways. One, the installer package will now attempt to open a Finder window for the /Application/Python 3.7 folder that contains the "Install Certificates.command". Two, rather than just a generic "installation complete" message at the end of the install, there is now a tailored message that urges the user to click on the "Install Certificates.command" icon. ...

0 _
  • 10,524
  • 11
  • 77
  • 109
Edward Ned Harvey
  • 6,525
  • 5
  • 36
  • 45
  • 2
    Is there a solution, if one doesn't have root access? Thanks. – tommy.carstensen May 18 '17 at 22:08
  • 4
    I ran the Install Certificates.command file but the error persists (in Py36) if I use an https URL in my code (to read content). If I use the same URL string minus the "s" (e.g., http), my code works. Unfortunately, I need https for some URLs. Any suggestion for this? (I also ran "pip3 install --upgrade certifi" because I need both Py27 and Py36 running) – ultrageek Feb 13 '18 at 00:57
  • For those without root; check out this answer: https://bugs.python.org/issue29095 – tommy.carstensen Feb 15 '18 at 19:04
  • Thanks a lot, spend hours to find a solution till reach this answer. Awesome. – mrblue May 01 '18 at 08:56
  • It's answers like these that spoil programmers ;) – Anurag Priyadarshi Jul 02 '18 at 03:29
  • 2
    This does not work; all errors persist, just as @ultrageek mentions. Also, please list the actual command instead of the alias that is bundled with a specific version of Python. -1. – hpm Dec 02 '18 at 12:39
  • @AaronCruz, The problem described here is a version-specific bundling bug, hence the link to version-specific script to fix it, and link to bug report that was closed in python 3.7. First try updating to latest python, or running the Install Certificates script. If that doesn't work, you're having a different problem, so next, ensure the server actually has the right certs - don't test cert validity with your browser, because browsers build caches of cert chains. Test with a tool like https://www.ssllabs.com/ssltest/ or similar. If none of these help you, I don't know what you need. – Edward Ned Harvey Dec 03 '18 at 20:41
  • Running `/Applications/Python\ 3.6/Install\ Certificates.command` in Terminal should work too. – David Douglas Feb 04 '19 at 15:10
  • 1
    Any idea what to do if I installed python using anaconda? I don't see Install Certificates.command anywhere – ucb Mar 27 '19 at 22:40
  • 20
    I don't have Python in my Applications folder – flppv Apr 09 '19 at 09:44
  • Could you mark this as the accepted answer? The problem is resolved in a way, at least closed in the python bug tracker. – creyD Jun 20 '19 at 16:10
  • @useranon did you ever figure it out? I also installed with Anaconda and can't seem to find the file everyone's talking about – Rohan Shah Sep 27 '20 at 13:44
  • @flppv you can run `which python3` to see where is this folder located. Then just `cd` in and use `open .` if you want to open the folder in your Finder. – gma992 Jun 30 '21 at 10:15
  • 4
    @gma992 I installed python through homebrew and pyenv. I don't see an Install Certificaters command in the directory where my python version is stored. – Khalil Hijazi Aug 10 '21 at 16:43
  • Using venv with Python 3.9 and had to double-click on Install Certificates.command – Erick Audet Feb 20 '23 at 02:40
58

I solved this problem using this command:

open /Applications/Python\ 3.7/Install\ Certificates.command

I have Python 3.7 in my machine.

Check this link - Fixing CERTIFICATE_VERIFY_FAILED error when trying requests-html out on Mac

ejderuby
  • 710
  • 5
  • 21
hyung ook An
  • 591
  • 4
  • 4
54

Cool way to solve this issue for all your python version and without checking your version on macOS

bash /Applications/Python*/Install\ Certificates.command

This command is equivalent to:

...
bash /Applications/Python\ 2.7/Install\ Certificates.command
bash /Applications/Python\ 3.6/Install\ Certificates.command
bash /Applications/Python\ 3.7/Install\ Certificates.command
...

It helped me hope it will help you as well

Oded BD
  • 2,788
  • 27
  • 30
  • % python3 get-pip.py --> WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)'))': /simple/pip/ Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, – Ashish Karpe Oct 25 '22 at 10:48
  • '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)'))) - skipping ERROR: Could not find a version that satisfies the requirement pip (from versions: none) – Ashish Karpe Oct 25 '22 at 10:48
  • bash /Applications/Python*/Install\ Certificates.command -- pip install --upgrade certifi /Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11: No module named pip Traceback (most recent call last): File "", line 44, in File "", line 24, in main File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/subprocess.py", line 413, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command ' – Ashish Karpe Oct 25 '22 at 10:49
  • ['/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11', '-E', '-s', '-m', 'pip', 'install', '--upgrade', 'certifi']' returned non-zero exit status 1. – Ashish Karpe Oct 25 '22 at 10:49
38

If pip does not fix the issue

pip3 install --upgrade certifi

Then try the following scripts if you can't find the "Install Certificates.command"

#!/usr/bin/env python3
# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module.  Uses the certificates provided by the certifi package:
#       https://pypi.python.org/pypi/certifi

import os
import os.path
import ssl
import stat
import subprocess
import sys

STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
             | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
             | stat.S_IROTH |                stat.S_IXOTH )


def main():
    openssl_dir, openssl_cafile = os.path.split(
        ssl.get_default_verify_paths().openssl_cafile)

    # +++> if already done  <----
    #print(" -- pip install --upgrade certifi")
    #subprocess.check_call([sys.executable,
    #    "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])

    import certifi
    # change working directory to the default SSL directory
    os.chdir(openssl_dir)
    relpath_to_certifi_cafile = os.path.relpath(certifi.where())
    print(" -- removing any existing file or link")
    try:
        os.remove(openssl_cafile)
    except FileNotFoundError:
        pass
    print(" -- creating symlink to certifi certificate bundle")
    os.symlink(relpath_to_certifi_cafile, openssl_cafile)
    print(" -- setting permissions")
    os.chmod(openssl_cafile, STAT_0o775)
    print(" -- update complete")

if __name__ == '__main__':
    main()
Andy Wang
  • 489
  • 5
  • 4
9

If you're using MacOS go to Applications >> python3.8 >> and double-click Install Certificates.command. This worked for me.

Ahmed J.
  • 484
  • 6
  • 11
7

In my case none of the solutions worked with the system installed python3 in macOS Catalina, neither did it work with python3 installed via brew.

If someone has a situation like this and wants a quick solution,
Download and install python3 again, using https://www.python.org/downloads/

At the end of the installation, the installer would show you a note, asking to run the Install Certificates.command file.
(With the other installations, this file was not present, and neither was the solution with the file's source code working)

Restart the terminal, and you can type where python3, to see /Library/Frameworks/Python.framework/Versions/3.8/bin/python3. Using this binary, the problem should not occur.

Note: It might be possible to make the system-installed python3 work, but in my case; it proved to be extremely hard, so I choose this way.

Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
  • 1
    This led me to the correct solution on Catalina when trying to get my bitbar python3 plugins working. I saw I had a couple python3 installations with `which python3` and changing the script shebang to `#!/usr/local/bin/python3` fixed the SSL errors. Thanks! – Michael Dec 10 '19 at 16:35
3

I faced the same problem, when I tried to run Python with Keras data loading. The error for me was:

Exception: URL fetch failure on AWS_URL: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)

I fixed my problem by upgrading the certificate as:

pip install --upgrade certifi
creyD
  • 1,972
  • 3
  • 26
  • 55
Ratha Pech
  • 31
  • 1
  • Hi! It is `pip install --upgrade certificate` isn't it? I made an edit to your post, but wasn't sure if this is a spelling mistake or a module I don't know. – creyD Jun 20 '19 at 14:44
  • Or is it this [module](https://pypi.org/project/certifi/)? Then please feel free to correct it! – creyD Jun 20 '19 at 16:13
  • 3
    It’s not a spelling mistake, but just a module name. – Ratha Pech Jun 22 '19 at 13:26
2

Confirm you are not in a virtualenv. I tried the above without success, only to realise my installations were failing because I was on a virtualenv

StackEdd
  • 624
  • 1
  • 10
  • 23
1

A cheap way around this is just using python3.5 if you still have it installed.

Pushing to PyPI:

python3.5 setup.py register -r pypitest

python3.5 setup.py sdist upload -r pypitest

pipping seems to work fine with 3.6 out of the box..

jarekwg
  • 375
  • 3
  • 8
0

For me, it was a misspecification of the request. I'd made a https call instead of a http call. Changing to http solved it.

ulle
  • 11
0

Ensure that you do not have SSL_CERT_FILE environment variable set. I had the same problem, it took a while before I figured out some application was setting this variable as an empty string inside my bash profile.

Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
0

I had the same issue with macOS Big Sur. Here is what I did to solve the issue.

IDE - Pycharm

Python Version Downloaded - 3.9.6

  • Download the latest python version and remove the earlier version
  • While installation, at the end (Summary Section), there is a small note to install an SSL Certificate. Read that Summary section carefully.
  • Double click the SSL Certificate path provided in the summary
  • It will take you to the respective folder where there should be one file named - "Install Certificate Command"
  • Double click on that file, it should open the terminal and will run the code automatically. Wait till you receive the message as "[Completed Successfully]".
  • Once done close and restart terminal/IDE and this should resolve your issue.

Note: if you have anaconda and python both installed on your system then check whether you are using the correct python version in the IDE which the latest version downloaded and not from Anaconda.

Enjoy.

rohan chikorde
  • 456
  • 5
  • 6
0

I needed to add my corporate CA certs into the python CA cert file due to corporate SSL gateways.

The location of the CA cert file is found by

import ssl
print(ssl.get_default_verify_paths().openssl_cafile)

The cert to be added was in keychain. After exporting it, it needed converting into PEM format so it could be cut & pasted into the cacert file -

openssl x509 -inform der -in cert.cer -out cert.pem
0

I did this and it worked on my Mac: Macintoch HD>Applications>Python file>Install Certificates.command (double click on it)

-1

Just REINSTALL your Python on your Mac

eastonsuo
  • 935
  • 9
  • 11
-1

sometimes if you're using conda or poetry you may be in a virtual environment shell. you can check with:

which python

for me the solution was as simple as cmd+t to open a new shell.

Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
-3

If you're using macOS open finder and go to Applications > Python3.7 folder (or whatever version of python you're using) > double click on "Install Certificates.command" file.