我正在使用名为'Tweetpony'的python库;一切正常,除了当我使用Pyinstaller打包我的脚本时,我在执行时收到以下错误:
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
我尝试按照这些人https://github.com/kennethreitz/requests/issues/557的建议在.spec文件中分配'caceret.pem' 但它没有帮助。
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()
答案 0 :(得分:4)
花了我几个小时才找到解决方案。我在Mac / El Capitan中收到了上述错误消息。 pip本身也行不通。我通过安装openssl并添加环境变量REQUESTS_CA_BUNDLE来解决它。
brew install openssl
export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/certs/cacert.pem
答案 1 :(得分:1)
您的问题是由Tweetpony
使用的请求模块引起的。
您必须向cacert.pem
和requests.get
函数提供requests.post
文件的路径。
您可以通过提供verify
参数或设置环境变量来完成此操作。
您可以在项目的GitHub问题部分找到修复程序: https://github.com/Mezgrman/TweetPony/issues/14
有关更多信息,请阅读本期“请求”模块: https://github.com/kennethreitz/requests/issues/557
代码也来自此链接。
#!/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文件:
# 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)