Python NameError:未定义全局名称“HTTPError”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15600707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameError: global name 'HTTPError' is not defined
提问by andy
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:
我在 try 子句中有一个命令,我知道它会引发异常。我试图在“except”子句中捕获它,但是except子句似乎无法识别异常的存在。异常在未处理时(即未包含在 try 子句中)在交互式窗口中如下所示:
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:Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:Python27\lib\urllib2.py", line 406, in open
response = meth(req, response)
File "C:Python27\lib\urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "C:Python27\lib\urllib2.py", line 444, in error
return self._call_chain(*args)
File "C:Python27\lib\urllib2.py", line 378, in _call_chain
result = func(*args)
File "C:Python27\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:
当我将命令放入该列表中第一个文件“actions.py”的 try/except 构造中时,如下所示:
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:
我希望上面的子句会在我运行 ncbo_annotate 函数时捕获“HTTPError: HTTP Error 500: Internal Server Error”,但是当我运行上面的函数时,我收到一条错误消息,说全局名称“HTTPError”是没有定义的:
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?
那么有什么关系呢?我认为 python 会引发异常,直到它在 try 子句中找到处理程序或将其吐出未处理。为什么我的代码不知道 HTTPError 是什么,或者我如何告诉它它是什么以便它可以处理它?
采纳答案by Emily
You probably just need to import the HTTPErrorclass before using it. Try inserting at the top of your actions.py file:
您可能只需要HTTPError在使用它之前导入该类。尝试在您的 actions.py 文件的顶部插入:
from urllib2 import HTTPError
and then you should be able to use your code as is.
然后您应该可以按原样使用您的代码。
回答by uselpa
You need to check for urllib2.HTTPError:
您需要检查 urllib2.HTTPError:
except urllib2.HTTPError:
回答by gerlos
@Emily's solution is fine, but there's another way to solve this problem without importing that class.
@Emily 的解决方案很好,但还有另一种方法可以在不导入该类的情况下解决此问题。
You just need to give the full name space of the exception class you want to catch:
你只需要给出你想要捕获的异常类的全名空间:
except urllib2.HTTPError:
This way you need fewer importclauses in your code, and it's easier for you to tell afterwards which module raised the exception.
这样你import的代码中就需要更少的子句,而且你之后更容易知道哪个模块引发了异常。
回答by kenorb
In Python 3 it's:
在 Python 3 中,它是:
from urllib.error import HTTPError

