属性错误:模块"请求"没有属性"post"。它是否已弃用并引入了新功能?



我一直在尝试将请求发送到使用flask构建的本地服务器。 请求是使用 Python 的requests模块发送的。

我不知道该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())

我为上述代码得到的错误是:

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

我的服务器代码是:

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)

我尝试检查文档,在线检查许多文章,并重写了整个代码。但是,没有一个帮助。

那么,requests.post函数是否已弃用并引入了一个新函数,或者我的代码是否有任何问题。

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

最新更新