NameError: name '__main__' is not defined

arvidurs picture arvidurs · Feb 18, 2014 · Viewed 36.8k times · Source

I have been reading here, but I couldnt find any solution online to solve my problem..I think I have the indentation right, but I still get the Name Error..Can someone help me out please. This script should run a new panel in maya, which works kind of, but the error is really annoying.

class PanelWindow( object ):
    def __init__( self, name, title, namespace=__name__ ):
        self.__name__ = name
        self._title = title
        self.instance = str(namespace) + '.' + self.__name__

        if not pm.scriptedPanelType(self.__name__, q = True, ex = True):
            pm.scriptedPanelType(self.__name__, u = True)

        jobCmd = 'python(\\\"%s._setup()\\\")' % self.instance
        job = "scriptJob -replacePrevious -parent \"%s\" -event \"SceneOpened\" \"%s\";" % ( self.__name__, jobCmd )
        mel.eval(job)

        pm.scriptedPanelType( self.__name__, e = True,
                       unique=True,
                       createCallback = 'python("%s._createCallback()")' % self.instance,
                       initCallback = 'python("%s._initCallback()"  )' % self.instance,
                       addCallback = 'python("%s._addCallback()"   )' % self.instance,
                       removeCallback = 'python("%s._removeCallback()")' % self.instance,
                       deleteCallback = 'python("%s._deleteCallback()")' % self.instance,
                       saveStateCallback = 'python("%s._deleteCallback()")' % self.instance
                        )


    def _setup(self):
        """Command to be call for new scene"""
        panelName = pm.sceneUIReplacement( getNextScriptedPanel=(self.__name__, self._title) )
        if panelName == '':
            try:
                panelName = pm.scriptedPanel( mbv=1, unParent=True, type=self.__name__, label=self._title )
            except:
                pass
        else:
            try:
                label = panel( self.__name__, query=True, label=True )
                pm.scriptedPanel( self.__name__, edit=True,  label=self._title )
            except:
                pass
    def _addCallback(self):
        """Create UI and parent any editors."""
        print 'ADD CALLBACK'
    def show( self ):        
        mel.eval('tearOffPanel "%s" %s true;' % (self._title, self.__name__) )


global test
test = PanelWindow('myName', 'Light')

test.show()


# NameError: name '__main__' is not defined # 
# Error: line 1: name '__main__' is not defined
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
# NameError: name '__main__' is not defined # 

Answer

Peter Gibson picture Peter Gibson · Feb 18, 2014

When executing Python scripts, the Python interpreter sets a variable called __name__ to be the string value "__main__" for the module being executed (normally this variable contains the module name).

It is common to check the value of this variable to see if your module is being imported for use as a library, or if it is being executed directly. So you often see this block of code at the end of modules:

if __name__ == '__main__':
    # do stuff

I suspect you have left the string quotes off of '__main__' which gives the NameError you're seeing

>>> if __name__ == __main__:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__main__' is not defined