I have installed searchtweets but it is showing error while importing
ImportError: cannot import name ‘gen_rule_payload’ from ‘searchtweets’

Import errors are usually due to incorrect Python environment - what endpoints are you trying to call and what package did you end up installing?

pip list

Will show you what’s installed

And I would check how you’re running the script to see if the environment you’re using is the same one you’ve installed packages in

Thank you so for reply
yes this is output of pip3 list
C:\Users\Admin>pip3 list
Package Version


argon2-cffi 20.1.0
astroid 2.5.1
async-generator 1.10
atomicwrites 1.4.0
attrs 20.3.0
backcall 0.2.0
beautifulsoup4 4.9.3
bleach 3.3.0
certifi 2020.12.5
cffi 1.14.5
chardet 4.0.0
colorama 0.4.4
coverage 5.5
cssselect 1.1.0
cycler 0.10.0
Cython 0.29.22
decorator 4.4.2
defusedxml 0.6.0
distython 0.0.3
entrypoints 0.3
future 0.18.2
fuzzy-c-means 1.0.0
GetOldTweets3 0.0.11
idna 2.10
imbalanced-learn 0.6.2
iniconfig 1.1.1
ipykernel 5.4.3
ipython 7.20.0
ipython-genutils 0.2.0
ipywidgets 7.6.3
isort 5.7.0
jedi 0.18.0
Jinja2 2.11.3
joblib 1.0.1
jsonschema 3.2.0
jupyter 1.0.0
jupyter-client 6.1.11
jupyter-console 6.2.0
jupyter-core 4.7.1
jupyterlab-pygments 0.1.2
jupyterlab-widgets 1.0.0
kiwisolver 1.3.1
lazy-object-proxy 1.5.2
lxml 4.6.3
MarkupSafe 1.1.1
matplotlib 3.3.4
mccabe 0.6.1
mistune 0.8.4
multi-imbalance 0.0.12
nbclient 0.5.2
nbconvert 6.0.7
nbformat 5.1.2
nest-asyncio 1.5.1
notebook 6.2.0
numpy 1.20.1
oauthlib 3.1.1
packaging 20.9
pandas 1.2.2
pandocfilters 1.4.3
parso 0.8.1
pickleshare 0.7.5
Pillow 8.1.2
pip 21.0.1
pluggy 0.13.1
prometheus-client 0.9.0
prompt-toolkit 3.0.16
py 1.10.0
pycparser 2.20
Pygments 2.8.0
pylint 2.7.2
pyparsing 2.4.7
pyquery 1.4.3
pyrsistent 0.17.3
PySocks 1.7.1
pyswarm 0.6
pyswarms 1.3.0
pytest 6.2.2
pytest-cov 2.11.1
python-dateutil 2.8.1
pytz 2021.1
pywin32 300
pywinpty 0.5.7
PyYAML 5.4.1
pyzmq 22.0.3
qdldl 0.1.5.post0
qtconsole 5.0.2
QtPy 1.9.0
requests 2.25.1
requests-oauthlib 1.3.0
scikit-learn 0.24.1
scipy 1.6.0
seaborn 0.11.1
searchtweets 1.7.4
searchtweets-v2 1.0.7
Send2Trash 1.5.0
setuptools 54.1.1
six 1.15.0
sklearn 0.0
snscrape 0.3.4
sortedcontainers 2.3.0
soupsieve 2.2.1
terminado 0.9.2
testpath 0.4.4
threadpoolctl 2.1.0
toml 0.10.2
tornado 6.1
tqdm 4.61.0
traitlets 5.0.5
tweepy 3.10.0
tweet-parser 1.13.2
urllib3 1.26.4
wcwidth 0.2.5
webencodings 0.5.1
wheel 0.36.2
widgetsnbextension 3.5.1
wrapt 1.12.1
WARNING: You are using pip version 21.0.1; however, version 21.1.2 is available.
You should consider upgrading via the ‘c:\users\admin\appdata\local\programs\python\python39\python.exe -m pip install --upgrade pip’ command.

You have both versions installed which may conflict. This is a bad idea - if you’re using v2 APIs, install searchtweets-v2 only, if using v1.1 Premium APIs, use searchtweets. You can pip uninstall searchtweets to remove a package for example.

Thanks it works for me . But now I am getting following error

premium_search_args = load_credentials(“twitter_keys.yaml”,
yaml_key=“search_tweets_api”,
env_overwrite=False)
rule = gen_rule_payload("#covid19 #vaccine lang:en", results_per_call=100, from_date=“2021-06-14”, to_date=“2021-06-16”)
print(rule)
{“query”: “#covid19 #vaccine lang:en”, “maxResults”: 100, “toDate”: “202106160000”, “fromDate”: “202106140000”}
premium_search_args = load_credentials(“twitter_keys.yaml”,
yaml_key=“search_tweets_api”,
env_overwrite=False)
rs = ResultStream(rule_payload=rule,
max_results=500,
max_pages=1,
**premium_search_args)

print(rs)
ResultStream:
{
“username”: null,

"rule_payload": {
    "query": "#covid19 #vaccine lang:en",
    "maxResults": 100,
    "toDate": "202106160000",
    "fromDate": "202106140000"
},
"tweetify": true,
"max_results": 500

tweets = list(rs.stream())
print(tweets)
HTTP Error code: 403: {“error”:{“message”:“Forbidden: Authentication succeeded but account is not authorized to access this resource.”,“sent”:“2021-06-18T17:37:54+00:00”,“transactionId”:“7e23daa9f766d8dc”}}
Request payload: {‘query’: ‘#covid19 #vaccine lang:en’, ‘maxResults’: 100, ‘toDate’: ‘202106160000’, ‘fromDate’: ‘202106140000’}

What endpoint did you configure in twitter_keys.yaml ?

endpoint int Twitter_keys.yaml https://api.twitter.com/1.1/tweets/search/fullarchive/academic.json

Just to be sure - you have v2 Academic Access correct? You were using it according to your other thread Twitter API 2 academic research track

If so, this is not the correct endpoint - for v2 academic access the yaml should look like this (with your own token):

# Academic
search_tweets_v2:
  endpoint: "https://api.twitter.com/2/tweets/search/all"
  bearer_token: "AAA...zzz"

I think twarc is slightly easier to use and configure vs search-tweets-python if you want to use it as a command line tool. But they will both produce the exact same output.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.