AttributeError: 'module' object has no attribute 'request'

大憨熊 提交于 2019-12-12 07:29:12

问题


When I run the following code in 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?


回答1:


Import urllib.request instead of urllib.

import urllib.request



回答2:


Interestingly, I noticed some IDE-depending behavior.

Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do

import urllib.request

while in Spyder,

import urllib

does fine




回答3:


If this is on PyCharm, as was mine, make sure your file name isn't urllib.py.




回答4:


  • In visual code , u have to write import urllib.request instead of just import urllib.
  • Also, whenever errors such as module x has no attribute y occurs, it's because you have named the current file same as the package you are trying to import.
  • So, the way import in python works is that it first searches the current dir, and if it finds the module/package 'x' u were looking for , it assumes that it has found the target file, and searches for 'y'. And since u haven't defined 'y', the aforementioned error occurs.


来源:https://stackoverflow.com/questions/22278993/attributeerror-module-object-has-no-attribute-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!