Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > AttributeError: 'module' object has no attribute 'urlopen'

Reply
Thread Tools

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

 
 
dont bother
Guest
Posts: n/a
 
      02-23-2004
Hey,
I am trying to run the following example from
diveintopython

import urllib
sock = urllib.urlopen("http://diveintopython.org/")
htmlSource = sock.read()
sock.close()
print htmlSource

I have Python 2.3.3 but when i run the above code I
get the following error:

raceback (most recent call last):
File "urllib.py", line 1, in ?
import urllib
File
"/home/shalen/programming/python/tutorials/py/urllib.py",
line 2, in ?
sock =
urllib.urlopen("http://diveintopython.org/")
AttributeError: 'module' object has no attribute
'urlopen'

Anyone knows how to fix this and why I am getting this
error?

Thanks
Dont



~
~
~
~


__________________________________
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-23-2004
dont bother wrote:

> import urllib
> sock = urllib.urlopen("http://diveintopython.org/")
> htmlSource = sock.read()
> sock.close()
> print htmlSource
>
> I have Python 2.3.3 but when i run the above code I
> get the following error:
>
> raceback (most recent call last):
> File "urllib.py", line 1, in ?
> import urllib
> File
> "/home/shalen/programming/python/tutorials/py/urllib.py",
> line 2, in ?
> sock =
> urllib.urlopen("http://diveintopython.org/")
> AttributeError: 'module' object has no attribute
> 'urlopen'
>


This is a name clash. As your script has the same name as the library
module, it imports itself instead of urllib in the library.

To fix it, just rename to, say,

/home/shalen/programming/python/tutorials/py/examineurllib.py

Peter
 
Reply With Quote
 
 
 
 
stewart
Guest
Posts: n/a
 
      02-24-2004
dont bother wrote:

> Anyone knows how to fix this and why I am getting this
> error?


well, the sample works fine in the shell. so the error is due to the
different way you refer to external modules when running from a file.


 
Reply With Quote
 
scriber scriber is offline
Junior Member
Join Date: Mar 2009
Posts: 1
 
      03-31-2009
I had the same problem, simply because I named my file "urllib.py" so it was calling itself!... that's a new one on me heheee
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class object's attribute is also the instance's attribute? 陈伟 Python 9 08-30-2012 03:20 PM
ATTRIBUTE ERROR: 'module' object has no attribute 'ssl' johnny Python 5 12-10-2006 01:10 PM
AttributeError: 'function' object has no attribute 'split' Earl Python 2 05-06-2004 05:05 PM
pythoncom - 'module' object has no attribute 'frozen' Paul Python 3 10-18-2003 07:31 AM
sys.argv[0] - 'module' object has no attribute 'argv' =?ISO-8859-1?Q?Thomas_N=FCcker?= Python 0 06-30-2003 02:07 PM



Advertisments