PyCharm中报错AttributeError:module 'requests' has no attribute 'get'求助
解决
AttributeError: module 'requests' has no attribute 'get'错误 运行以下调用Kanye名言API的Python代码时,出现AttributeError: module 'requests' has no attribute 'get'错误:
from tkinter import * import requests def get_quote(): response = requests.get('https://api.kanye.rest') response.raise_for_status() data = response.json() quote = data["quote"] canvas.itemconfig(quote_text, text=quote) window = Tk() window.title("Kanye Says...") window.config(padx=50, pady=50) canvas = Canvas(width=300, height=414) background_img = PhotoImage(file="background.png") canvas.create_image(150, 207, image=background_img) quote_text = canvas.create_text(150, 207, text="Kanye Quote Goes HERE", width=250, font=("Arial", 30, "bold"),fill="white") canvas.grid(row=0, column=0) kanye_img = PhotoImage(file="kanye.png") kanye_button = Button(image=kanye_img, highlightthickness=0.1, command=get_quote) kanye_button.grid(row=1, column=0) window.mainloop()
解决方法:
- 检查脚本文件名:不要将自己的Python脚本命名为
requests.py,这会与官方requests模块重名,导致导入的是本地脚本而非官方模块。将脚本改名(比如kanye_quote.py),同时删除目录下自动生成的requests.pyc文件(如果存在)。 - 确认requests模块安装状态:在PyCharm终端执行
pip show requests,查看模块是否已正确安装。若未安装,执行pip install requests重新安装。 - 验证PyCharm解释器配置:通过
File → Settings → Project: [项目名] → Python Interpreter查看当前解释器的已安装包列表,确保requests在其中。没有的话点击+号搜索并安装。 - 清理PyCharm缓存:点击
File → Invalidate Caches...,选择Invalidate and Restart重启IDE,清除可能存在的模块缓存问题。
内容的提问来源于stack exchange,提问作者Shivam Mishra
相关产品推荐
相关产品推荐

