[英]AttributeError: 'module' object has no attribute 'request'
When I run the following code in Python 3.3:当我在 Python 3.3 中运行以下代码时:
import urllib
tempfile = urllib.request.urlopen("http://yahoo.com")
I get the following error:我收到以下错误:
I did this too to verify:我也这样做来验证:
What am I doing wrong?我究竟做错了什么?
The urllib
module has been split into parts and renamed in Python 3 to urllib.request
, urllib.parse
, and urllib.error
. urllib
模块已被拆分为多个部分,并在 Python 3 中重命名为urllib.request
、 urllib.parse
和urllib.error
。
Import urllib.request
instead of urllib
.导入
urllib.request
而不是urllib
。
import urllib.request
Interestingly, I noticed some IDE-depending behavior.有趣的是,我注意到了一些依赖于 IDE 的行为。
Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do Spyder 和 PyCharm 在我的机器上使用相同的解释器:在 PyCharm 中我需要做
import urllib.request
导入 urllib.request
while in Spyder,在 Spyder 时,
import urllib
导入 urllib
does fine很好
如果这是在 PyCharm 上,就像我的一样,请确保您的文件名不是 urllib.py。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.