By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,583 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.