views:

53

answers:

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.

+3  A: 

Perhaps this is another example of why it is better to import modules than functions from modules.

OptionGroup is defined in the module optparse. The command

from optparse import OptionParser

puts OptionParser in the global namespace, but neglects OptionGroup entirely.

To fix the code, import the optparse module, and access its parts like so:

import optparse
parser = optparse.OptionParser()
outputGroup = optparse.OptionGroup(parser, 'Output handling')
unutbu
precisely the problem. and I agree with the "better way to import modules" but I actually *fixed it* going along with the docs and using `from optparse import OptionParser, OptionGroup` - btw, took you long enough! ;D
Cawas