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. Advertisements

  2. dont bother

    Peter Otten Guest

    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. Advertisements

  3. dont bother

    stewart Guest

    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
    Likes Received:
    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
     
    scriber, Mar 31, 2009
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.