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

D

dont bother

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
 
P

Peter Otten

dont said:
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
 
S

stewart

dont said:
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.
 
Joined
Mar 31, 2009
Messages
1
Reaction score
0
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top