a name error

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 15 02:18:17 EDT 2008


En Tue, 15 Apr 2008 02:54:43 -0300, Penny Y. <pylists at arcor.de> escribió:

> import urllib2,sys
> try:
>     r=urllib2.urlopen("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.URLError, e: ...

Or import both things from urllib2:

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

-- 
Gabriel Genellina




More information about the Python-list mailing list