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

Discussion in 'Python' started by dont bother, Feb 23, 2004.

  1. dont bother

    dont bother Guest

    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
     
    dont bother, Feb 23, 2004
    #1
    1. Advertising

  2. dont bother

    Peter Otten Guest

    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
     
    Peter Otten, Feb 23, 2004
    #2
    1. Advertising

  3. dont bother

    stewart Guest

    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.
     
    stewart, Feb 24, 2004
    #3
  4. dont bother

    scriber

    Joined:
    Mar 31, 2009
    Messages:
    1
    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
     
    scriber, Mar 31, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page