4

I'm using a python library called 'Tweetpony'; everything works fine except for that when I use Pyinstaller to package my script, I receive the following error upon execution:

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've tried allocating the 'caceret.pem' in the .spec file as advised 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()
Bowdzone
  • 3,827
  • 11
  • 39
  • 52
Demitri
  • 41
  • 1
  • 1
  • 3

2 Answers2

5

Took me hours to find the solution. I got the above error message in Mac/El Capitan. Also pip itself would not work. I solved it by installing openssl, and adding environment variable REQUESTS_CA_BUNDLE.

brew install openssl export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/certs/cacert.pem

mbdev
  • 6,343
  • 14
  • 46
  • 63
1

Your issue is caused in the requests module used by Tweetpony. You have to provide the path to the cacert.pem file to the requests.get and the requests.post functions. You can do this by providing the verify parameter or by setting the environment variable.

You can find the fix on the GitHub issue section of the project: https://github.com/Mezgrman/TweetPony/issues/14

For more information read this issue of 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)
user937284
  • 2,454
  • 6
  • 25
  • 29