名称“选项组”没有定义
此错误是严格按照文档中找到的示例来完成的。而且你在任何地方都找不到任何关于它的说明,无论是那个长长的文档页面< /a>, google或stackoverflow。另外,阅读 optparse.py 会显示 OptionGroup 存在,因此这会增加混乱。
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
>>> from optparse import OptionParser
>>> outputGroup = OptionGroup(parser, 'Output handling')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'OptionGroup' is not defined
我敢打赌不到 1 分钟就会有人发现我的错误。 :)
是的,这意味着我知道答案,但由于我花了很长时间才发现,所以我想在这里“记录”它。
This error is done strictly by following examples found on the docs. And you can't find any clarification about it anywhere, be it that long long docs page, google or stackoverflow. Plus, reading optparse.py
shows OptionGroup is there, so that adds to the confusion.
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
>>> from optparse import OptionParser
>>> outputGroup = OptionGroup(parser, 'Output handling')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'OptionGroup' is not defined
I bet it will take less than 1 minute for someone to spot my error. :)
Yes, that means I knew the answer, but since this took me so long to discover I wanted to "document" it here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这是另一个例子说明为什么它是导入模块比从模块导入函数更好。
OptionGroup
在模块optparse
中定义。命令
from optparse import OptionParser
将
OptionParser
放入全局命名空间,但完全忽略OptionGroup
。要修复代码,请导入 optparse 模块,并像这样访问其部分:
Perhaps this is another example of why it is better to import modules than functions from modules.
OptionGroup
is defined in the moduleoptparse
.The command
from optparse import OptionParser
puts
OptionParser
in the global namespace, but neglectsOptionGroup
entirely.To fix the code, import the
optparse
module, and access its parts like so: