gpt4 book ai didi

python - 使用 URL 请求时遇到问题

转载 作者:太空狗 更新时间:2023-10-30 00:49:01 25 4
gpt4 key购买 nike

我只是写了下面的代码来玩转 Requests 库

requests tests
import requests
r = requests.get('https://api.github.com/events')

但我一直收到相同的错误消息,即使我使用 from requests import *

Traceback (most recent call last):
File "/Users/dvanderknaap/Desktop/Organized/CS/My_Python_Programs/requests.py", line 3, in <module>
import requests
File "/Users/dvanderknaap/Desktop/Organized/CS/My_Python_Programs/requests.py", line 5, in <module>
r = requests.get('https://api.github.com/events')
AttributeError: 'module' object has no attribute 'get'

我已经尝试使用 pip install requests 重新安装请求,但输出是:

Requirement already satisfied (use --upgrade to upgrade): requests in /anaconda/lib/python3.5/site-packages

我认为问题是它安装在我的 python3.5 库中,但我使用的是 python2.7,但我不确定如何修复它。有什么建议吗?

最佳答案

首先,将文件 My_Python_Programs/requests.py 重命名为 requests.py 以外的名称。它正在导入自身而不是请求模块。

您的 python 2.7 可能已安装也可能未安装请求包。如果没有,您可以使用

安装它
pip2.7 install requests

关于python - 使用 URL 请求时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com