2017年4月21日 星期五

[python] slackclient python 2.6 "python": "Python/{v.major}.{v.minor}.{v.micro}".format(v=sys.version_info), AttributeError: 'tuple' object has no attribute 'major'


slackclient  在 python 2.6 的的環境下使用會出現下列錯誤

root@hc2[/usr/lib/python2.6/site-packages/slackclient]{21:05}
# python ~/workspace/slackbot/disk_bot.py
Traceback (most recent call last):
 File "/root/workspace/slackbot/disk_bot.py", line 31, in <module>
   sc = SlackClient(slack_token)
 File "/usr/lib/python2.6/site-packages/slackclient/_client.py", line 28, in __init__
   self.server = Server(self.token, False)
 File "/usr/lib/python2.6/site-packages/slackclient/_server.py", line 27, in __init__
   self.api_requester = SlackRequest()
 File "/usr/lib/python2.6/site-packages/slackclient/_slackrequest.py", line 21, in __init__
   "python": "Python/{v.major}.{v.minor}.{v.micro}".format(v=sys.version_info),
AttributeError: 'tuple' object has no attribute 'major'

Error message 中指出, 是 sys.version_info 那些參數在 python 2.6 中沒有提供
修改如下

/usr/lib/python2.6/site-packages/slackclient/_slackrequest.py

import platform
from .version import __version__
            
            
class SlackRequest(object):
   def __init__(self):
            
       # __name__ returns 'slackclient._slackrequest', we only want 'slackclient'
       client_name = __name__.split('.')[0]
       client_version = __version__  # Version is returned from version.py
            
       # Construct the user-agent header with the package info, Python version and OS versi
       self.default_user_agent = {
           "client": "{0}/{1}".format(client_name, client_version),
           #"python": "Python/{v.major}.{v.minor}.{v.micro}".format(v=sys.version_info),   
           "python": "Python/{v[0]}.{v[1]}.{v[2]}".format(v=sys.version_info),
           "system": "{0}/{1}".format(platform.system(), platform.release())
       }    
            
       self.custom_user_agent = None

Usage of sys.version_info.major causes crash on python < 2.7 · Issue #79 · astraw/stdeb


沒有留言:

張貼留言