当我输入以下内容时,它表明我已经安装了请求模块:pip3 install requests
响应:
Requirement already satisfied: requests in /usr/local/lib/python3.7/site-packages (2.24.0)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests) (2.9)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests) (2019.11.28)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests) (3.0.4)
因此,当我使用代码创建名为http.py的文件时
import requests
url = "https://google.com"
response = requests.get(url)
print(f"your request to {url} came back w/ status code {response.status_code}")
我收到一条错误消息:
Traceback (most recent call last):
File "23_Making-HTTP-Requests/http.py", line 2, in <module>
import requests
File "/usr/local/lib/python3.7/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/local/lib/python3.7/site-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 11, in <module>
from .exceptions import (
File "/usr/local/lib/python3.7/site-packages/urllib3/exceptions.py", line 2, in <module>
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 668, in _load_unlocked
File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 199, in load_module
mod = mod._resolve()
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 113, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 82, in _import_module
__import__(name)
File "/Users/colechalland/Code/Python-3/Modern_Python3_Bootcamp/23_Making-HTTP-Requests/http.py", line 5, in <module>
response = requests.get(url)
AttributeError: module 'requests' has no attribute 'get'
但是当我使用python2运行此代码时:
import requests
url = "https://google.com"
response = requests.get(url)
print(response.status_code)
我没有任何错误并打印出:200
我尝试使用pip3卸载并重新安装请求,但这不能解决问题。奇怪的是,当我在终端中使用相同的代码运行python3时,它可以工作:
Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> url = "https://google.com"
>>> response = requests.get(url)
>>>
>>> print(f"your request to {url} came back w/ status code {response}")
your request to https://google.com came back w/ status code <Response [200]>
>>>
答案 0 :(得分:3)
问题是您的http.py
文件,只需将其重命名为其他文件即可。有一个http package,基础库(urllib3
)正在从中导入一些模块。
答案 1 :(得分:0)
您的cwd中是否有一个名为requests.py的文件?如果存在,则应重命名它,并确保它与python的任何其他模块都不匹配,否则会出现错误。