NLTK download SSL: Certificate verify failed
Asked Answered
P

15

99

I get the following error when trying to install Punkt for nltk:

nltk.download('punkt')    
 [nltk_data] Error loading Punkt: <urlopen error [SSL:
 [nltk_data]     CERTIFICATE_VERIFY_FAILED] certificate verify failed
 [nltk_data]     (_ssl.c:590)>
False
Physicalism answered 12/8, 2016 at 11:4 Comment(1)
Before posting yet another answer: we already have an answer that says run the .command file in the Python directory on Mac from 2017. We don't need another one that just changes the python version number of the directory.Vibrator
J
28

The downloader script is broken. As a temporal workaround can manually download the punkt tokenizer from here and then place the unzipped folder in the corresponding location. The default folders for each OS are:

  • Windows: C:\nltk_data\tokenizers
  • OSX: /usr/local/share/nltk_data/tokenizers
  • Unix: /usr/share/nltk_data/tokenizers
Jasminjasmina answered 25/8, 2016 at 10:27 Comment(1)
This helped a lot. But for my case, the zip file should have been placed in the directory /nltk_data/corpora. Detailed answer here: https://mcmap.net/q/215837/-nltk-download-ssl-certificate-verify-failedSlump
I
204

TLDR: Here is a better solution: https://github.com/gunthercox/ChatterBot/issues/930#issuecomment-322111087

Note that when you run nltk.download(), a window will pop up and let you select which packages to download (Download is not automatically started right away).

To complement the accepted answer, the following is a complete list of directories that will be searched on Mac (not limited to the one mentioned in the accepted answer):

    - '/Users/YOUR_USERNAME/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - '/Users/YOUR_USERNAME/YOUR_VIRTUAL_ENV_DIRECTORY/nltk_data'
    - '/Users/YOUR_USERNAME/YOUR_VIRTUAL_ENV_DIRECTORY/share/nltk_data'
    - '/Users/YOUR_USERNAME/YOUR_VIRTUAL_ENV_DIRECTORY/lib/nltk_data'

In case the link above dies, here is the solution pasted in its entirety:

import nltk
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

nltk.download()

Run the above code in your favourite Python IDE or via the command line.

Improvisation answered 18/5, 2018 at 8:10 Comment(3)
Yes, the download when to my Users directory path and it worked like a charm. Very helpful since I did not have admin privileges. I actually had tried the accepted answer with the help of an admin standing over my shoulder, and that did not work for me.Corney
The solution didn't work for me and it may not be enough for a separate answer on here, BUT, for the record and what has worked for me is to disable my VPN. In other words, a connection may sometimes not be established with this error message in return, if the case of your VPN is blacklistted etc.Hellbox
For some reason I wasn't able to click on- and download the module I needed from the launched window. It just didn't install. To fix that I used the exact same code as above but added the identifier inside nltk.downlaod(). The model I needed had the identifier "punkt", hence replacing nltk.download() with nltk.download('punkt') by-passed the window and downloaded my package. Other than that, a perfect solution, thanks, i'm upvoting.Ldopa
C
68

This works by disabling SSL check!

import nltk
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

nltk.download()
Cloistered answered 25/4, 2019 at 5:55 Comment(1)
this is more reliable than looking for commands that go missing every OS-X update.Ballance
I
45

Run the Python interpreter and type the commands:

import nltk
nltk.download()

from here: http://www.nltk.org/data.html

if you get an SSL/Certificate error, run the following command

bash '/Applications/Python 3.6/Install Certificates.command'

(Obviously, different Python versions have come out since then, so change 3.6 to whatever version is installed.)

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

Iaea answered 12/9, 2017 at 3:7 Comment(3)
I like this solution better than the accepted answer, since it involves making Python’s SSL checks actually work instead of disabling SSL cert checks altogether. Probably low risk, but the accepted answer appears to expose you to a MITM attack.Poltroon
For who is running Python3.10...just run this command "/Applications/Python 3.10/Install Certificates.command"Thirza
Thanks for the answer bash /Applications/{python_version}/Install\ Certificates.commandObliquely
G
31

Search 'Install Certificates.command' in the finder and open it.

Then do the following steps in the terminal:

python3
import nltk
nltk.download()
Glossology answered 30/12, 2019 at 12:14 Comment(0)
J
28

The downloader script is broken. As a temporal workaround can manually download the punkt tokenizer from here and then place the unzipped folder in the corresponding location. The default folders for each OS are:

  • Windows: C:\nltk_data\tokenizers
  • OSX: /usr/local/share/nltk_data/tokenizers
  • Unix: /usr/share/nltk_data/tokenizers
Jasminjasmina answered 25/8, 2016 at 10:27 Comment(1)
This helped a lot. But for my case, the zip file should have been placed in the directory /nltk_data/corpora. Detailed answer here: https://mcmap.net/q/215837/-nltk-download-ssl-certificate-verify-failedSlump
K
7

You just need to Install the certificate doing this simple step

In the python application folder double-click on the file 'Certificates.command'

this will make a prompt window show in your screen and basically will automatically install the certificate for you, close this window and try again.

Kling answered 28/8, 2019 at 20:6 Comment(0)
H
4

My solution is:

  • Download punkt.zip from here and unzip
  • Create nltk_data/tokenizers folders under home folder
  • Put punkt folder under tokenizers folder
Hann answered 2/1, 2019 at 8:27 Comment(0)
O
3

There is a very simple way to fix all of this as written in the formal bug report for anyone else coming across this problem recently (e.g. 2019) and using MacOS. From the bug report at https://bugs.python.org/issue28150:

...there is a simple double-clickable or command-line-runnable script ("/Applications/Python 3.6/Install Certificates.command") that does two things: 1. uses pip to install certifi and 2. creates a symlink in the OpenSSL directory to certifi's installed bundle location.

Simply running the "Install Certificates.command" script worked for me on MacOS (10.15 beta as of this writing) and I was off and running.

Otherworldly answered 18/8, 2019 at 23:6 Comment(4)
Using just released MacOS 10.15 and was unable to find "Install Certificates.command" file. There is no /Applications/Python 3.x directory.Perbunan
Which version of Python are you using?Otherworldly
Python 3.7.3 from /usr/bin/python3 pre-installed on macOS 10.15Perbunan
I'm also using Python 3, but not pre-installed. Perhaps that is the difference. Try installing another version maybe and seeing if the directory exists for you to take these actions. Either that or the directory was changed in later versions (after 3.6), though I doubt it. I'll verify when I get back to my Mac.Otherworldly
P
2

My solution after nothing worked. I navigated, via the GUI to the Python 3.7 folder, opened the 'Certificates.command' file in terminal and the SSL issue was immediately resolved.

Padre answered 8/1, 2019 at 10:27 Comment(0)
B
2

A bit late to the party but I just entered Certificates.command into Spotlight which found it and ran it. All fixed in seconds.

I'm running mac Catalina and using python 3.7 installed by Homebrew

Bellbird answered 7/7, 2020 at 10:1 Comment(0)
A
1

It means that you are not using HTTPS to work consistently with other run time dependencies for Python etc.

If you are using Linux (Ubuntu)

~$ sudo apt-get install ca-certificates

Should solve the issue.

If you are using this in a script with a docker file, you have to make sure you have install the the ca-certificates modules in your docker file.

Abba answered 26/12, 2016 at 4:26 Comment(0)
G
0

First go to the path /Applications/Python 3.6/ and run Install Certificates.command

You will admin rights for the same.

If you are unable to download it, then as other answer suggest you can download directly and place it. You need to place them in the following directory structure.

> nltk_data
          > corpora
                   > brown
                   > conll2000
                   > movie_reviews
                   > wordnet
          > taggers
                   > averaged_perceptron_tagger
          > tokenizers
                      > punkt
Glia answered 15/1, 2018 at 9:16 Comment(0)
T
0

For mac: Search Install Certificates.command in the finder and open it.

After successfully finished, just start download

import nltk
nltk.download('wordnet')
Tweeny answered 27/7, 2023 at 16:8 Comment(0)
S
0

I am using Python version 3.12.1 and nltk version 3.8.1 on MacOS. Download the package you want from here, yours would be punkt. Search it in that page and click on the download.

Next, you do not need to unzip it, just copy and paste it in the "correct" directory:

  • OSX: /User/YourUsername/nltk_data/corpora

(For other systems, just search nltk_data and find the dicrectory.)

Note: I just chosen one of the directories where it tried to search for the package. I received this error when tried to use the package:

Attempted to load corpora/stopwords

  Searched in:
    - '/Users/YourUsername/nltk_data'
    - '/Library/Frameworks/Python.framework/Versions/3.12/nltk_data'
    - '/Library/Frameworks/Python.framework/Versions/3.12/share/nltk_data'
    - '/Library/Frameworks/Python.framework/Versions/3.12/lib/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'

I just chosen the first one.

Slump answered 16/2, 2024 at 16:37 Comment(0)
A
-1

For me, the solution was much simpler: I was still connected to my corporate network/VPN which blocks certain types of downloads. Switching the network made the SSL error disappear.

Avidin answered 21/11, 2022 at 10:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.