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

Peter Otten __peter__ at web.de
Mon Feb 23 03:30:15 EST 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



More information about the Python-list mailing list