Re: a name error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabriel Genellina

    Re: a name error

    En Tue, 15 Apr 2008 02:54:43 -0300, Penny Y. <pylists@arcor. deescribió:
    import urllib2,sys
    try:
    r=urllib2.urlop en("http://un-know-n.com/")
    except URLError,e:
    print str(e)
    sys.exit(1)
    >
    print r.info()
    >
    >
    But got the errors:
    >
    Traceback (most recent call last):
    File "t1.py", line 4, in ?
    except URLError,e:
    NameError: name 'URLError' is not defined
    Same as the function urlopen, you have to qualify URLError with the module
    first:

    except urllib2.URLErro r, e: ...

    Or import both things from urllib2:

    from urllib2 import urlopen, URLError
    try:
    r = urlopen(...)
    except URLError, e:
    ...

    --
    Gabriel Genellina

  • mimi.vx

    #2
    Re: a name error

    or

    import sys
    from urllib2 import *
    try:
    r=urllib2.urlop en("http://un-know-n.com/")
    except URLError,e:
    print str(e)
    sys.exit(1)

    print r.info()

    se python scope and namespaces ..

    Comment

    Working...