I am trying to access the python newsapi and I have registered, and gotten an api key. However, even when copy-pasting the code from the website into my code, it returns an import error. I am trying to make an app and this is causing a great inconvenience.
Traceback (most recent call last):
File "main.py", line 1, in <module>
from newsapi import NewsApiClient
ImportError: cannot import name 'NewsApiClient' from 'newsapi' (/home/runner/CAC-Draft/venv/lib/python3.8/site-packages/newsapi/__init__.py)
My code:
from newsapi import NewsApiClient
# Init
newsapi = NewsApiClient(api_key='[API_KEY]')
# /v2/top-headlines
top_headlines = newsapi.get_top_headlines(q='bitcoin',
sources='bbc-news,the-verge',
category='business',
language='en',
country='us')
# /v2/everything
all_articles = newsapi.get_everything(q='bitcoin',
sources='bbc-news,the-verge',
domains='bbc.co.uk,techcrunch.com',
from_param='2017-12-01',
to='2017-12-12',
language='en',
sort_by='relevancy',
page=2)
# /v2/top-headlines/sources
sources = newsapi.get_sources()
I have put in a valid api key, I just kept it hidden to protect my account.
I set the PYTHONPATH to '.' and that solved it for me. export PYTHONPATH='.'
Since you're using a Python interpreter from a virtual environment (venv), make sure you installed newsapi-python
in the venv.
source venv/bin/activate
pip install newsapi-python
python <run your app>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.