简体   繁体   中英

cannot import name requests

I was trying to run the following code but it shows me an error

Traceback (most recent call last): File "/home/python/flask_ass.py", line 1, in from flask import Flask,requests,json ImportError: cannot import name 'requests'

My code snippet is

from flask import Flask, requests, json

# api key=03a84ce64ef242418d262c11a57248de
url = ('https://newsapi.org/v2/top-headlines?'
       'country=us&'
       'apiKey=03a84ce64ef242418d262c11a07248de')
params = {'id': 21,
          'name': 'John'}
response = requests.get(url, params=params)

r_dict = response.json()
# print(r_dict)
# print(response.status_code())
print(r_dict['articles'][1]['title'])
# print(response.text)

app = Flask(__name__)


@app.route("/")
def hell():
    return r_dict

json and requests are not part of Flask .

from flask import Flask, requests, json

should be

import json
import requests
from flask import Flask

You have a typo,

try the singular:

from flask import Flask, request, json

BTW, despite the downvotes, I checked and yes, from flask import Flask, json works.

UPDATE: As @DeepSpace mentioned, it's possible that the poster wanted to import requests & json directly.

from flask import Flask
import requests, json

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM