SSL: CERTIFICATE_VERIFY_FAILED in python while installing package [duplicate] - python

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

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.

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()

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
from here: ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

Search 'Install Certificates.command' in the finder and open it.
Then do the following steps in the terminal:
python3
import nltk
nltk.download()

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

This is how I solved it for MAC OS.
Initially after installing nltk, I was getting the SSL error.
Solution:
Goto
cd /Applications/Python\ 3.8
Run the command
./Install\ Certificates.command
Now if you try again, it should work!
Thanks a lot to this article!

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.

My solution is:
Download punkt.zip from here and unzip
Create nltk_data/tokenizers folders under home folder
Put punkt folder under tokenizers folder

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.

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.

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

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.

For mac users,
just copy paste the following in the terminal:
/Applications/Python\ 3.10/Install\ Certificates.command ; exit;

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

Updating the python certificates worked for me.
At the top of your script, keep:
import nltk
nltk.download('punkt')
In a separate terminal run (Mac):
bash /Applications/Python <version>/Install Certificates.command

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.

Related

python 'speedtest' has no attribute 'Speedtest'

I am trying to get the download and upload speed in python by using the "speedtest" module, but it gives me that error when I use the module:
AttributeError: module 'speedtest' has no attribute 'Speedtest'.
and I was only declaring the variable,
that is my code :
import speedtest
speedtester = speedtest.Speedtest()
The module actually doesn't have the functions for some reason.
Please tell me what is wrong with my code I was sure to import the module from the cmd and also the pycharm terminal and still got the same error.
Thanks in advance
I had the same issue. I was using PyCharm IDE.
The issue occurs when you install speedtest using pip install speedtest
In order to solve the above mentioned issue, you need to use the following command.
pip install speedtest-cli
But before doing this, uninstall the previous one by using pip uninstall speedtest
Screenshot to installation
I was getting the same error. Then I google the problem and eventually came here. Later I realised that I have named my python file speedtest.py. I renamed it to something else (which is not the name of any python module) and it works just fine now.
<--Screenshot-->
So make sure this case.
If you installed "speedtest" and "speedtest-cli" libraries together then this issue will arise.
So first uninstall "speedtest" library using "pip uninstall speedtest" command.
Then try your code like :
"import speedtest
st = speedtest.Speedtest()
print(st.download()/1024)"
You will get output
i was also facing the same issue , what i have done is :
i have uninstall the speedtest and speedtest-cli
pip uninstall speedtest
pip uninstall speedtest-cli
then again install speedtest-cli
pip install speedtest-cli
The issue got solved after moving the speedtest.py file to the same directory as my script and it worked just fine. So just make sure the file is in the same folder as your python script.
Bumped into the issue and investigated the content of the module...
It appears what inside my venv folder I got a module folder "speedtest" with EMPTY init.py file and that is it...
Next to the folder was speedtest.py with actual code...
So deleting the empty folder/module helped me...
You should uninstall before speedtest whit the command 'pip uninstall speedtest' . After that, this use this code to find download and upload, speeds and ping:
import speedtest
test = speedtest.Speedtest()
print("Loading server list...")
test.get_servers()
print("Choosing best server...")
best = test.get_best_server()
print(f"Found: {best['host']} located in {best['country']}")
print("Performing download test...")
download_result = test.download()
print("Performing upload test...")
upload_result = test.upload()
ping_result = test.results.ping
print(f"Download speed: {download_result / 1024 / 1024:.2f}Mbit/s")
print(f"Upload speed: {upload_result / 1024 / 1024:.2f}Mbit/s")
print(f"Ping: {ping_result}ms")
I was facing the same problem,but main problem might be you are installing speedtest library which is not suitable instead use pip install speedtest-cli but before that remember to delete the previous one using pip uninstall speedtest and check the spelling of get_servers which is in the plural form

Python can't find pyobdc. Could the name of the Folder be the Problem?

I am running 64-bit Windows 7, python 3.7.
I used pip install pyodbc and it installed pyodbc-4.0.28.dist-info version in the following directory in my pc: C:\Users\Owner\AppData\Local\Programs\Python\Python37\Lib\site-packages.
Since I got the following warning - import pyodbc ImportError: DLL load failed: the specified module could not be found - so I used the sys.path to find out whether the path to it was there or not and it was>
C:\Users\Propietario\AppData\Local\Programs\Python\Python37\Lib\idlelib
C:\Users\Propietario\Python Scripts
C:\xampp\htdocs\Python Scripts
C:\Users\Propietario\AppData\Local\Programs\Python\Python37\python37.zip
C:\Users\Propietario\AppData\Local\Programs\Python\Python37\DLLs
C:\Users\Propietario\AppData\Local\Programs\Python\Python37\lib
C:\Users\Propietario\AppData\Local\Programs\Python\Python37
**C:\Users\Propietario\AppData\Local\Programs\Python\Python37\lib\site-packages**
As it can be seen the path to the directory is included in the PATH variable.
I'm wondering if the problem is the directory's name? it's not pyodbc, but pyodbc-4.0.28.dist-info
By the way, pip uninstall pyodbc had no problem finding it
Uninstalling pyodbc-4.0.28:
Would remove:
c:\users\Owner\appdata\local\programs\python\python37\lib\site-package
s\pyodbc-4.0.28.dist-info*
c:\users\Owner\appdata\local\programs\python\python37\lib\site-package
s\pyodbc.cp37-win_amd64.pyd
Proceed (y/n)? y
Successfully uninstalled pyodbc-4.0.28
Does anyone have any idea about it. What can I do to solve the problem
Thanks in adavance
it's not you. it failed for me too. on a mac.
so doing a little digging - it's an outstanding issue -
https://github.com/mkleehammer/pyodbc/issues/677
https://github.com/mkleehammer/pyodbc/issues/663
return import("pyodbc") ImportError: DLL load failed: The specified module could not be found
the solution is "I am also encountering the same problem with pyodbc 4.0.28 while with 4.0.27 everything works as intended." Tatu Leinonen copyrite
I did not face issue with python but with maven. My observation is:
Some time there are issues with Get-Item -Path command or SETX PATH in PowerShell. You could try to set path variable through this script (run as admin) and try.
$existingpath = $env:Path
$newpath = $existingpath + ';' + '<Your Path>'
[System.Environment]::SetEnvironmentVariable('Path', $newpath, [System.EnvironmentVariableTarget]::Machine )
As a # ShpielMeister said, there's an issue with pyodbc version 4.0.28, so I decided to go with version 4.0.27 for now.
So for those of you who are new to python and pyodbc like me, the following code is the way to get a specific version:
pip install pyodbc==4.0.27
This version works just fine for me.

SSL: CERTIFICATE_VERIFY_FAILED -> trouble opening up a web page [duplicate]

I need to use curtom root certificates on the company intranet and loading them in the Mac OS TrustStore (KeyChain) does solve the problem for all browsers and GUI apps.
It seems that it works even with the version of curl that ships with Mac OS X but it doesn't work with python, even the version that ships with Mac OS 10.12 Sierra (Python 2.7.10)
Still, it seems that I would be hit by:
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>
How can I solve this?
Because I encounter this issue in lots and lots of Python tools I would really appreciate if I find a way to avoid it without having to patch them.
Providing the custom CA certificate myself is not an option because I cannot patch tens of Python tools that I use.
Most of the tools are using the requests library but there are a few that are using the native ssl support in Python directly.
This is also a problem in Python 3.6 with MacOS Sierrra. I know your use case is different. But I stumbled upon this thread while investigating this problem. So if anyone is also having this article is worth checking out:
http://www.cdotson.com/2017/01/sslerror-with-python-3-6-x-on-macos-sierra/
In a nutshell: Python 3.6 does not rely on MacOS' openSSL anymore. It comes with its own openSSL bundled and doesn't have access on MacOS' root certificates.
You have two options:
Run an install command shipped with Python 3.6
cd /Applications/Python\ 3.6/
./Install\ Certificates.command
or
Install the certifi package with
pip install certifi
I chose the first option and it worked.
Run this to set the appropriate variables. This is a combination of the answers that have already been given here. Put it in your ~/.bash_profile to make it permanent.
CERT_PATH=$(python -m certifi)
export SSL_CERT_FILE=${CERT_PATH}
export REQUESTS_CA_BUNDLE=${CERT_PATH}
Mac brew install python env.
$ python3
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import certifi
>>> certifi.where()
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/certifi/cacert.pem'
>>>
Or from the command line:
$ python -m certifi
then need link cacert.pem as cert.pem
$ ln -s /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/certifi/cacert.pem cert.pem
$ pwd
/Library/Frameworks/Python.framework/Versions/3.7/etc/openssl
rehash
then work fine.
If you put the additional certificates in a PEM bundle file you can use these two environment variables to overwrite the default cert stores used by Python openssl and requests.
SSL_CERT_FILE=/System/Library/OpenSSL/cert.pem
REQUESTS_CA_BUNDLE=/System/Library/OpenSSL/cert.pem
Please note that this file does not exist, you need to build it yourself.
This was intended as an edit to an existing question, but since the queue was already full, posting as a separate answer.
Tested on MacOS 12.3.1 with python 3.10 installed with MacPorts.
If you prefer to trust root CA according to your OS, export them from System Roots keychain into a single file:
security export -t certs -f pemseq -k /System/Library/Keychains/SystemRootCertificates.keychain -o bundleCA.pem
If, additionally, you want to trust some internal self-signed CAs, export them, too. They are probably stored under the System keychain:
security export -t certs -f pemseq -k /Library/Keychains/System.keychain -o selfSignedCAbundle.pem
Merge the two files:
cat bundleCA.pem selfSignedCAbundle.pem >> allCAbundle.pem
Export as bash variable
export REQUESTS_CA_BUNDLE=/path/to/allCAbundle.pem
Consider adding the last code snippet to your .bash_profile
Note that REQUESTS_CA_BUNDLE works only for a single file, and does not for a directory.
I see a lot of answers out there recommend to turn off certificate validation or to use certifi.where.
While turning off SSL is obvious risk. certifi.where is also a risk, mainly if you intend to make this code a production code that will run in a customer env.
PEP describing why it is wrong.
ssl.create_default_context is well integrate with linux and windows trustStore. the problem is, as in your case with mac.
I solve this by loading the certificates using the integrated security commandline tool
def create_macos_ssl_context():
import subprocess
import ssl
import tempfile
ctx = ssl.create_default_context()
macos_ca_certs = subprocess.run(["security", "find-certificate", "-a", "-p",
"/System/Library/Keychains/SystemRootCertificates.keychain"],
stdout=subprocess.PIPE).stdout
with tempfile.NamedTemporaryFile('w+b') as tmp_file:
tmp_file.write(macos_ca_certs)
ctx.load_verify_locations(tmp_file.name)
print(ctx.get_ca_certs())
Note that this gives you the systemRoot certificates. if you need the user than simply change the value in the security command
As an update and datapoint, I ran into this issue running Python 3.7.0 on macOS 10.13.4:
$ ipython
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import bokeh.sampledata
In [2]: bokeh.sampledata.download()
Using data directory: /Users/me/.bokeh/data
...
SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1045)
Instructions for solving the problem are in /Applications/Python\ 3.7/ReadMe.rtf
Following the suggestion there and running /Applications/Python\ 3.7/Install\ Certificates.command solved the problem:
From the terminal:
$ /Applications/Python\ 3.7/Install\ Certificates.command
Re-starting IPython...
$ ipython
>>> import bokeh.sampledata
>>> bokeh.sampledata.download()
Using data directory: /Users/me/.bokeh/data
Downloading: CGM.csv (1589982 bytes)
1589982 [100.00%]
...
For me /Applications/Python\ 3.6/./Install\ Certificates command fails on pip certifi install. I am on mac High Sierra and use python3 so pip somewhat fails and I have to use pip3 instead.
So here what I did:
Manually ran pip3 install --update certify in a shell
Remove the install certifi line from the command script
Reran the script and everything was fine.
Note that you will end up with a cert.pem symbolic link in: /Library/Frameworks/Python.framework/Versions/3.6/etc/openssl/
Solution for MacOS or Linux with latest Python versions installed either as standalone or via port or brew
Download certificates from Certifi project at https://github.com/certifi/python-certifi/blob/master/certifi/cacert.pem. FYI Certifi is a 3rd party library that provides Mozilla’s curated collection of Root Certificates for validating the trustworthiness of SSL certificates while verifying the identity of TLS hosts.
Then add to your ~/.zshrc on the latest MacOS or ~/.bash_profile or similar:
export SSL_CERT_FILE=/pathtodownloadedfile/cacert.pem
export REQUESTS_CA_BUNDLE=/pathtodownloadedfile/cacert.pem

downloading error using nltk.download()

I am experimenting NLTK package using Python. I tried to downloaded NLTK using nltk.download(). I got this kind of error message. How to solve this problem? Thanks.
The system I used is Ubuntu installed under VMware. The IDE is Spyder.
After using nltk.download('all'), it can download some packages, but it gets error message when downloading oanc_masc
To download a particular dataset/models, use the nltk.download() function, e.g. if you are looking to download the punkt sentence tokenizer, use:
$ python3
>>> import nltk
>>> nltk.download('punkt')
If you're unsure of which data/model you need, you can start out with the basic list of data + models with:
>>> import nltk
>>> nltk.download('popular')
It will download a list of "popular" resources.
Ensure that you've the latest version of NLTK because it's always improving and constantly maintain:
$ pip install --upgrade nltk
EDITED
In case anyone is avoiding errors from downloading larger datasets from nltk, from https://stackoverflow.com/a/38135306/610569
$ rm /Users/<your_username>/nltk_data/corpora/panlex_lite.zip
$ rm -r /Users/<your_username>/nltk_data/corpora/panlex_lite
$ python
>>> import nltk
>>> dler = nltk.downloader.Downloader()
>>> dler._update_index()
>>> dler._status_cache['panlex_lite'] = 'installed' # Trick the index to treat panlex_lite as it's already installed.
>>> dler.download('popular')
And if anyone wants to find nltk_data directory, see https://stackoverflow.com/a/36383314/610569
And to config nltk_data path, see https://stackoverflow.com/a/22987374/610569
From command line, after importing nltk, try
nltk.download('popular', halt_on_error=False)
After an error it will ask to retry broken package, just decline with n and it will continue with proper packages.
a) in OSX either run
sudo /Applications/Python\ 3.6/Install\ Certificates.command
b) switch to admin user (the one you have set up with administrator privileges)
and type at command line:
/Applications/Python\ 3.6/Install\ Certificates.command
Notes:
"\" are necessary because they escape blank characters in file names.
This procedure worked if you have python 3.6 installed, otherwise
change it in order to match your install python version... for this
execute:
ls /Applications
and look at the python directory name you have there.
An easy(hard) way to get over this error is to do the process manually. Just go to the website https://www.nltk.org/nltk_data/ and download the required zip file and extract the contents.
In Windows, go to user/AppData/local/Programs/Python/Python(version)/lib and create a folder nltk_data. Then create the respective folder. As an example, for 'punkt' create the folder tokenizers and add the folder 'punkt' inside the extracted folder to it. This info is mostly given by the terminal itself.
Run your program. Cheers!
EDIT 1: Of course, downloading all files can be time-consuming, but it's the only option if the "urlopen error" persists.
EDIT 2 It is also mostly your router or network at fault that you are not able to download nltk files. Try changing your network and that should help.
I had this error:
Resource punkt not found. Please use the NLTK Downloader to obtain the resource: import nltk nltk.download('punkt')
When I tried to solve by writing:
import nltk
nltk.download()
my computer shut downs suddenly and anaconda also closed. When I try to open it always shows an error.
I solved the problem by writing:
import nltk
nltk.download('punkt')

Installing python-nmap package on Mac OS

I am trying to install nmap package on my mac. After I had completed the installation process described on the developer's site I tried to run the following code:
import nmap
def nmapScan(tgtHost, tgtPort):
nmScan = nmap.PortScanner()
So I got an error:
File "build/bdist.macosx-10.10-intel/egg/nmap/nmap.py", line 180, in __init__
nmap.nmap.PortScannerError: 'nmap program was not found in path. PATH is :/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
Ok. I found the path to python-nmap package and added it manually to the PATH. I believe that I have to add one that contains nmap.py file. Here it is:
/Users/%username%/Documents/Python_docs/python-nmap-0.1.4/nmap
And I am still getting the same error. Does anyone know why? Is there someone else who had this problem before? I have searched through internet and there is nothing that helps me. Thank you!
You need to install the nmap first. Use this link nmap. Since the Python library invokes the nmap binary. nmap binary should be available in your system.
Install nmap with homebrew and this error goes away.
brew install nmap
You can do this before or after you install python-nmap.

Categories