AttributeError:在Ubuntu中运行Python tweepy脚本时解码错误

时间:2018-01-31 12:17:19

标签: python ubuntu error-handling tweepy attributeerror

我有一个Python 3.5脚本本质上是一个Twitter刮刀,它使用 tweepy 包及其 StreamListener 函数收集推文。

现在我的脚本在Command shell中完美运行但是当我尝试在我的服务器的Ubuntu环境中运行它时,我收到解码错误。

   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 445, in filter
self._start(async)
   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 361, in _start
self._run()
   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 294, in _run
raise exception
   File "/usr/lib/python3/dist-packages/tweepy/streaming.py", line 247, in _run
verify=self.verify)
   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 454, in reque                                                                                                             st
prep = self.prepare_request(req)
   File "/usr/lib/python3/dist-packages/requests/sessions.py", line 388, in prepa                                                                                                             
re_request
hooks=merge_hooks(request.hooks, self.hooks),
   File "/usr/lib/python3/dist-packages/requests/models.py", line 297, in prepare
self.prepare_auth(auth, url)
   File "/usr/lib/python3/dist-packages/requests/models.py", line 490, in prepare                                                                                                             _auth
   r = auth(self)
   File "/usr/lib/python3/dist-packages/requests_oauthlib/oauth1_auth.py", line 7                                                                                                             1, in __call__
r.url = to_native_str(r.url)
   File "/usr/lib/python3/dist-packages/requests_oauthlib/oauth1_auth.py", line 1                                                                                                             4, in to_native_str
return string.decode('utf-8')

AttributeError: 'str' object has no attribute 'decode'

这是有道理的,因为您无法解码已经是字符串的字符串。但我的问题是如何解决这个问题,因为这是一个包内发生的错误?

1 个答案:

答案 0 :(得分:0)

看起来问题来自 requests-oauthlib ,您运行的是旧版本,问题似乎已经从4.0.1开始修复。

升级您的系统(Ubuntu 16.10的打包版本为7.0):

sudo apt update && apt upgrade

或使用pip安装:

sudo pip3 install requests-oauthlib