You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tweepy 3.8.0中OAuthHandler导入失败问题求助

Troubleshooting Your Tweepy Follower Fetch Issue

Let's work through this problem step by step to fix the ModuleNotFoundError and other hidden issues in your code:

1. Fix the OAuthHandler Import Path

In Tweepy 3.8.0, the OAuthHandler class lives directly under the root tweepy module—not in tweepy.auth. That's exactly why you're getting the "module not found" error.

Replace this line:

from tweepy.auth import OAuthHandler

With either:

from tweepy import OAuthHandler

Or skip the explicit import entirely and reference it directly:

auth = tweepy.OAuthHandler(privatekey.TWITTER_APP_KEY, privatekey.TWITTER_APP_SECRET)

2. Fix the Authentication Method Typo

You have a missing 's' in the access token method name. set_acces_token should be set_access_token:

auth.set_access_token(privatekey.TWITTER_KEY, privatekey.TWITTER_SECRET)

3. Fix the Infinite Recursion in Your get_followers Function

Right now, your function calls itself immediately with no exit condition—this will cause a stack overflow error once you fix the import issues. Here's a corrected version using Tweepy's Cursor (the recommended way to handle paginated data like follower lists):

def get_followers(api, username):
    followers = []
    # Use Cursor to auto-handle pagination
    for page in tweepy.Cursor(api.followers, screen_name=username).pages():
        followers.extend(page)
        print(f"Fetched {len(followers)} followers so far")  # Optional progress tracker
    return followers

# Call the function correctly outside its definition
followers_list = get_followers(api, "harrispolicy")

4. Verify Your Tweepy Installation

Double-check that Tweepy 3.8.0 is installed in the exact environment you're running your code (critical for virtual environments or Jupyter notebooks):

pip show tweepy

If it shows a different version, uninstall and reinstall the correct release:

pip uninstall tweepy
pip install tweepy==3.8.0

Quick Additional Checks

  • Confirm your privatekey.py is in the same folder as your main script, and all credential variable names match exactly (e.g., TWITTER_APP_KEY is correctly defined).
  • Ensure your Twitter API credentials have read access permissions (required to fetch follower lists with API v1.1, which Tweepy 3.8.0 uses).

内容的提问来源于stack exchange,提问作者Seplo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:07:21