Name "OptionGroup" not defined

This error is executed strictly following the examples found in the documents . And you cannot find any clarification about this anywhere, be it this long docs , google or stackoverflow page . Also, reading optparse.py shows that OptionGroup exists, which 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 would take less than a minute for someone to find my mistake. :)

Yes, this means that I knew the answer, but since it took me so long to find that I wanted to "document" it here.

+4
source share
1 answer

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

OptionGroup defined in the optparse module. Command

from optparse import OptionParser

puts OptionParser in the global namespace, but completely ignores OptionGroup .

To fix the code, import the optparse module and access its parts as follows:

 import optparse parser = optparse.OptionParser() outputGroup = optparse.OptionGroup(parser, 'Output handling') 
+6
source

Source: https://habr.com/ru/post/1309702/


All Articles