Kep*_*ond 1 python urllib python-import python-2.7
当尝试Request从urllib.requestPython 2.7 导入时,它无法找到包.
>>> from urllib.request import Request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named request
Run Code Online (Sandbox Code Playgroud)
urllib.requestPython 2中没有模块,该模块仅存在于Python 3中.
urllib2改为使用:
from urllib2 import Request
Run Code Online (Sandbox Code Playgroud)
从模块顶部的文档:
注意:该
urllib2模块已经拆分为Python 3中的几个模块命名urllib.request和urllib.error.2to3将源转换为Python 3时,该工具将自动调整导入.
如果您正在学习某种教程或课程,您可能需要安装Python 3并继续使用该版本; 您尝试执行的代码显然是为Python 3设计的.