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.
Perhaps this is another example of why it is better to import modules than functions from modules.
OptionGroupis defined in the moduleoptparse.The command
from optparse import OptionParserputs
OptionParserin the global namespace, but neglectsOptionGroupentirely.To fix the code, import the
optparsemodule, and access its parts like so: