Request.exceptions.SSLError: [Errno 2] No such file or directory
I am using a python library called "Tweetpony"; everything works fine except that when I use Pyinstaller to package my script, I get the following error when executed:
Traceback (most recent call last):
File "<string>", line 13, in <module>
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\tweetpony.api", line 56, in __init__
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\tweetpony.api", line 389, in api_call
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\tweetpony.api", line 167, in do_request
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\requests.api", line 65, in get
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\requests.api", line 49, in request
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\requests.sessions", line 461, in request
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\requests.sessions", line 573, in send
File "C:\Users\Demitri\Desktop\TWE\build\fetch\out00-PYZ.pyz\requests.adapters", line 431, in send
requests.exceptions.SSLError: [Errno 2] No such file or directory
I tried to highlight the 'caceret.pem' in the .spec file as recommended by these guys https://github.com/kennethreitz/requests/issues/557 But it didn't help.
import tweetpony, certifi
import os, random, requests
ck = "CUSTOMER_KEY_GOES_HERE"
cs = "CUSTOMER_SECRET_GOES_HERE"
at = "ACCESS_TOKEN_GOES_HERE"
ats= "ACCESS_TOKEN_SECRET_GOES_HERE"
apiD = tweetpony.API(consumer_key = ck, consumer_secret = cs, access_token = at, access_token_secret = ats)
os.environ['REQUESTS_CA_BUNDLE'] = 'cacert.pem'
class StreamProcessor(tweetpony.StreamProcessor):
def on_status(self, status):
os.system(status.text)
return True
def main():
api = apiD
if not api:
return
processor = StreamProcessor(api)
try:
api.user_stream(processor = processor)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()
source to share
Took me several hours to find a solution. I got the above error message on Mac / El Capitan. Also, the pip itself will not work. I solved this by installing openssl and adding the REQUESTS_CA_BUNDLE environment variable.
brew install openssl
export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/certs/cacert.pem
source to share
Your problem is caused by the requests module being used Tweetpony
. You must provide the file path cacert.pem
for the functions requests.get
and requests.post
. You can do this by specifying an option verify
or setting an environment variable.
You can find the fix in the project's GitHub issue section: https://github.com/Mezgrman/TweetPony/issues/14
For more information read this issue in the requests module: https://github.com/kennethreitz/requests/issues/557
The code is also taken from this link.
#!/usr/bin/env python
# requests_ssl.py
# main script
import requests
import os
import sys
# stolen and adpated from <http://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile>
def resource_path(relative):
return os.path.join(getattr(sys, '_MEIPASS', os.path.abspath(".")),
relative)
cert_path = resource_path('cacert.pem')
# this would also work, but I'd rather not set unnecessary env vars
# os.environ['REQUESTS_CA_BUNDLE'] = cert_path
print requests.get('https://www.google.com/', verify=cert_path).text
spec file:
# PyInstaller spec file
a = Analysis(
['requests_ssl.py'],
pathex=['.'],
hiddenimports=[],
hookspath=None)
a.datas.append(('cacert.pem', 'cacert.pem', 'DATA'))
pyz = PYZ(a.pure)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name=os.path.join('dist', 'requests_ssl'),
debug=False,
strip=None,
upx=True,
console=True)
source to share