NameError:未定义全局名称'HTTPError' [英] NameError: global name 'HTTPError' is not defined

查看:486
本文介绍了NameError:未定义全局名称'HTTPError'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在try子句中有一个命令,我知道会引发异常。我试图在except子句中捕获它,但except子句似乎无法识别异常的存在。在未处理的情况下(即未包含在try子句中),例外情况如下所示:

  Traceback(大部分最近一次调用的最后一次):
在< module>文件中的< console>行1,
文件C:\ Users \Andy\software\Turkeys\actions.py,行234,注释
annotation = annotator.ncbo_annotate(thing)
文件 C:\ Users \Andy\software\Turkeys\annotator.py,第49行,在ncbo_annotate
fh = urllib2.urlopen(submitUrl,postData)
文件C:\ 32Python27\lib\urllib2.py,第126行,在urlopen
中返回_opener.open(url,data,timeout)
文件C:\ 32Python27\lib\urllib2.py ,第406行,打开
response = meth(req,response)
在http_response
中的文件C:\ 32Python27\lib\urllib2.py,第519行, http',request,response,code,msg,hdrs)
文件C:\ 32Python27\lib\urllib2.py,第444行,错误
返回self._call_chain(* args )
在_call_chain
result = func(* args)
文件C:\ 32Python27\lib\urllib2.py,第378行,文件C:\ 32Python27 \\ \\ lib \urllib2.py,行527,在http_error_default
中引发HTTPError(req.get_full_url(),code,msg,hdrs,fp)
HTTPError:HTTP错误500:内部服务器错误

当我将该命令放入该列表中第一个文件的try / except构造时,actions.py,如下所示:

 尝试:
annotation = annotator.ncbo_annotate(thing)
除HTTPError:
...做一些这个
的东西

我期望上面的子句能够捕捉到HTTPError:HTTP Error 500:Internal服务器错误是在我运行ncbo_annotate函数时产生的,但是当我运行上面的代码时,出现错误提示没有定义全局名称HTTPError:

 Traceback(最近一次调用最后一次):
在< module>中,第1行的文件< console>
文件C:\Users\Andy\software\Turkeys\actions.py,第235行,注释
,但HTTPError:
NameError:全局名称'HTTPError'没有定义

那么交易是什么?我以为python会引发异常,直到它在try子句中找到一个处理程序,或者将它抛出未处理。为什么我的代码不知道HTTPError是什么,或者,我怎么告诉它它是什么以便它能够处理它? 您可能只需要在使用它之前导入 HTTPError 类。尝试在actions.py文件的顶部插入:

  from urllib2 import HTTPError 



然后您应该可以按原样使用您的代码。


I have a command in a try clause which I know throws an exception. I'm trying to catch it in an "except" clause, but the except clause seems to not recognize the existence of the exception. The exception, when unhandled (i.e. not enclosed in a try clause), looks like this in the interactive window:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Andy\software\Turkeys\actions.py", line 234, in annotate
    annotation=annotator.ncbo_annotate(thing)
  File "C:\Users\Andy\software\Turkeys\annotator.py", line 49, in ncbo_annotate
    fh = urllib2.urlopen(submitUrl, postData)
  File "C:\32Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\32Python27\lib\urllib2.py", line 406, in open
    response = meth(req, response)
  File "C:\32Python27\lib\urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\32Python27\lib\urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "C:\32Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\32Python27\lib\urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error

The when I put the command in a try/except construct in the first file in that list, "actions.py", like this:

try:
    annotation=annotator.ncbo_annotate(thing)
except HTTPError: 
    ...do some things with this 

I would expect that the above clause would catch the "HTTPError: HTTP Error 500: Internal Server Error" being produced when I run the ncbo_annotate function, but instead when I run the above, I am getting an error saying global name "HTTPError" is not defined:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Andy\software\Turkeys\actions.py", line 235, in annotate
    except HTTPError:
NameError: global name 'HTTPError' is not defined

So what's the deal? I thought python raises the exception until it finds a handler within a try clause or spits it out unhandled. Why does my code not have any idea what an HTTPError is, or alternatively, how do I tell it what it is so that it can handle it?

解决方案

You probably just need to import the HTTPError class before using it. Try inserting at the top of your actions.py file:

from urllib2 import HTTPError

and then you should be able to use your code as is.

这篇关于NameError:未定义全局名称'HTTPError'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆