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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dont bother

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

    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.


  • Peter Otten

    #2
    Re: AttributeError: 'module' object has no attribute 'urlopen'

    dont bother wrote:
    [color=blue]
    > 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'
    >[/color]

    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.p y

    Peter

    Comment

    • stewart

      #3
      Re: AttributeError: 'module' object has no attribute 'urlopen'

      dont bother wrote:
      [color=blue]
      > Anyone knows how to fix this and why I am getting this
      > error?[/color]

      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.


      Comment

      Working...