AttributeError: 模块“requests”没有属性“post”。是否已弃用并引入了新功能?

Nav*_*ala 0 python rest localhost flask python-requests

我一直在尝试向使用 Flask 构建的本地服务器发送请求。请求是使用requestspython 模块发送的。

我不知道该requests.post功能是否已被弃用并引入了新功能,或者我的代码是否有任何问题。我已经完全按照本文中所说的做了所有事情。
这是我的代码:

import requests

host_url = "http://127.0.0.1:5000"

# get the data
data_for_prediction = [int(input()) for _ in range(10)]
r = requests.post(url=host_url,json={data_for_prediction})
print(r.json())
Run Code Online (Sandbox Code Playgroud)

我在上面的代码中得到的错误是:

Traceback (most recent call last):
  File "C:/Users/--/requests.py", line 1, in <module>
    import requests
  File "C:\Users\--\requests.py", line 8, in <module>
    r = requests.post(url=host_url,json={data_for_prediction})
AttributeError: module 'requests' has no attribute 'post'

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

我的服务器代码是:

flask_server_app = Flask(__name__)

# let's make the server now
@flask_server_app.route("/api/predict", methods=["GET", "POST"])
# my prediction function goes here
def predict():
    # Get the data from the POST request & reads the received json
    json_data = request.get_json(force=True)

    # making prediction
    # Make prediction using model loaded from disk as per the data.
    prediction = ml_model.predict([[json_data]])

    # return json version of the prediction
    return jsonify(prediction[0])

# run the app now
if __name__ == '__main__':
    flask_server_app.run(port=5000, debug=True)
Run Code Online (Sandbox Code Playgroud)

我试过检查文档,在网上查了很多文章,还重新编写了整个代码。但是,没有任何帮助。

那么,该requests.post功能是否已弃用并引入了新功能,或者我的代码有什么问题。

sph*_*ere 8

似乎您正在一个名为的文件中编写代码,requests.py因此当您尝试导入 requests 模块时,它确实将您自己的文件作为模块导入。尝试重命名您的文件...