Display Line errors Python in Maya

I am starting to learn python and I was wondering if there was a way to display line errors in the script editor like in MEL? Right now I keep getting things like this

Error: unexpected indent

Thanks.

Sorry because my answer will be pretty unhelpful, but I don’t think I ever had to enable or change anything in Maya to get line errors working.
What version are you on?

In the script editor, do you have History > Show Stack Trace enabled?

If you can’t find that menu, right click in the history pane and it’s under the radial popup… and make sure that History > Line numbers in errors is on too. You’ll notice though that the line numbers are wrong if you execute only a selection of text… I avoid the script editor like the plague because of junk like that.

You can also customize error printouts by overwriting the function formatGuiException in maya.utils. Here’s the help for that function:



formatGuiException(exceptionType, exceptionObject, traceBack, detail=2)
    Format a trace stack into a string.
    
        exceptionType   : Type of exception
        exceptionObject : Detailed exception information
        traceBack       : Exception traceback stack information
        detail          : 0 = no trace info, 1 = line/file only, 2 = full trace
                          
    To perform an action when an exception occurs without modifying Maya's 
    default printing of exceptions, do the following::
    
        import maya.utils
        def myExceptCB(etype, value, tb, detail=2):
            # do something here...
            return maya.utils._formatGuiException(etype, value, tb, detail)
        maya.utils.formatGuiException = myExceptCB

So, you can get any error printout you like by stuffing your own function into maya.utils.formatGuiException:


def alternate_error_handler ( tb_type, exc_object, tb, detail=2 ):
    return "no error message for you"
    
# stuff our function into the utils module:
utils.formatGuiException = alternate_error_handler

#this triggers an error:
raise ValueError("I am a custom error")

# Error: no error message for you # 

Usually you’ll want to do something a little more helpful. The documentation on the traceback moduleshows you how to take apart a traceback and get good info out of it

This is part of what our custom excepthook does – the example won’t work cut and paste but should give you an idea of what can be done:


def excepthook( tb_type, exc_object, tb, detail=2 ):
    '''
    If the custom_except_hook flag is true, this replaces the maya global exception hook
    '''

    db_file = tb.tb_frame.f_code.co_filename
    db_names = tb.tb_frame.f_code.co_names


        # we don't do error reproting on problems that occur in the script listener
        # -- these are handled the old Maya way

    if db_file != "<maya console>":

        try:
            #generate a bug email and send it
            EH = EmailErrorHandler()
            is_menu_callback = ( 'mayaMenus' in db_names )
            EH.send( tb_type, exc_object, tb, verbose=not is_menu_callback )
    
        except:
            print "# Something bad happened inside the error reporting system"
            print "# Means you should go get Steve!"
    
            # something went wrong in the error handler itself. Try to send _that_!
    
            EH = ErrorReporter()
            t, o, tb = sys.exc_info()
    
            from .context import DebugContext
            ctx = DebugContext( t, o, tb )
    
            error_msg = email.MIMEText.MIMEText( ctx.formatHTML(), 'html' )
            error_msg['subject'] = "Error handling failure"
            EH.send_error_email( "Error handling failure", error_msg )
    
            #and let's confuse the user by printing out the error in the listener too...
    
            print "#--------------------------------------------------------"
            print traceback.format_exc()
            print "#--------------------------------------------------------"

    # pass the original error to the standard maya printout too...
    return utils._formatGuiException( tb_type, exc_object, tb, detail )

Steve that’s a very useful piece of code and concept. Installing excepthooks in python is super useful. Thanks for posting these examples.

original credit to Nathan Horne"s website :wink: