(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【Python/tweepy】tweepy.Clientを利用するためにtweepyのバージョンをあげる手順

スポンサーリンク

やりたいこと

Twitter API v2経由でツイートをするためにtweepy.Clientを利用したい。
ただし、現状のバージョンでは以下のエラーとなるためPython/tweepyのバージョンアップが必要。

# python3 test.py 
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    client = tweepy.Client(BT, AK, AS, AT, ATS)
AttributeError: module 'tweepy' has no attribute 'Client'
環境情報
  • Debian GNU/Linux 9
  • Python 3.5.3
  • tweepy 3.9.0

やり方

tweepyに関して最新モジュールをクローンした上で

# git clone https://github.com/tweepy/tweepy.git

以下の手順でインストールを行うと

# cd tweepy
# pip3 install .
Processing /root/tweepy
Collecting oauthlib<4,>=3.2.0
  Downloading oauthlib-3.2.2-py3-none-any.whl (151 kB)
     |████████████████████████████████| 151 kB 4.4 MB/s 
...
Successfully installed certifi-2022.12.7 charset-normalizer-3.1.0 idna-3.4 oauthlib-3.2.2 requests-2.28.2 requests-oauthlib-1.3.1 tweepy-4.13.0 urllib3-1.26.15...

tweepyのバージョンアップが完了する。

# pip3 show tweepy
Name: tweepy
Version: 4.13.0

尚、インストール時にPythonのバージョン不足でエラーになった場合は先に新しいバージョンのPythonインストールを行う必要がある。

# wget https://www.python.org/ftp/python/3.9.2/Python-3.9.2.tar.xz
# tar xJf Python-3.9.2.tar.xz
# cd Python-3.9.2
# ./configure
# make
# make install

バージョンアップ確認は再度ログインすること可能。

# python3 --version
Python 3.9.2

以下、補足です。

補足

Twitterにてv2のAPIを利用してツイートをするために同モジュールのアップデートを行いました。

$ cat test.py 
import tweepy
# BT, AK, AS, AT, ATS を定義
client = tweepy.Client(BT, AK, AS, AT, ATS)
def create_tw(msg):
    response = client.create_tweet(text=msg)
create_tw("test")

私の環境ではPythonのアップデートをせずにpip installをすると下記メッセージと共にエラーとなったので

# pip install .
DEPRECATION: Python 3.5 reached the end of its life on September 13th, 2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021. pip 21.0 will remove support for this functionality.
Processing /root/tweepy
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-igcdg27o/setup.py'"'"'; __file__='"'"'/tmp/pip-req-build-igcdg27o/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-q1hp5zyk
 ...
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

同様のメッセージが出る方は同じようにPythonのアップデートを先にしてみてください。
以上。