当我尝试使用python 3中的请求模块执行简单的程序时,它给了我以下错误,这似乎与模块本身有关。
Traceback (most recent call last):
File "req.py", line 1, in <module>
import requests
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/__init__.py", line 11, in <module>
from . import exceptions
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/exceptions.py", line 3, in <module>
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 914, in _find_spec
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/packages/six.py", line 192, in find_spec
return spec_from_loader(fullname, self)
File "<frozen importlib._bootstrap>", line 422, in spec_from_loader
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/packages/six.py", line 222, in is_package
return hasattr(self.__get_module(fullname), "__path__")
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/packages/six.py", line 121, in __getattr__
_module = self._resolve()
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/packages/six.py", line 118, in _resolve
return _import_module(self.mod)
File "/Users/redacted/Library/Python/3.8/lib/python/site-packages/urllib3/packages/six.py", line 87, in _import_module
__import__(name)
File "/Users/redacted/Desktop/python/http1/http.py", line 1
HTTP -- hypertext transfer protocol
^
SyntaxError: invalid syntax
程序是最简单的get请求。我不知道发生了什么。
看起来错误来自以下行:
HTTP -- hypertext transfer protocol
Python试图将其解析为代码,因此您应该将其注释:
# HTTP -- hypertext transfer protocol
或者作为字符串:
"HTTP -- hypertext transfer protocol"
另外,我会避免将文件命名为http.py
,因为这是Python标准库模块的名称。