Best way to exit early from a Python node

Is there a way to gracefully exit early from a Python node, if, for example, an error is encountered? In a script one would use sys.exit, but that gives a stack trace in the console, which might obscure any log message written about the error.

Hi @ccdc_knime,

There is no similarly simple way that I know of. You could put your entire script into a function and add a call to that function at the end of the script. Inside the function you could then return early. So something like:

def my_script():
    # Do stuff
   if error:
        # Exit early
        return

my_script()

I like the idea of using sys.exit the same way in Python nodes. I will add a feature request for KNIME to gracefully handle SystemExit exceptions as a way to terminate script execution early.

Marcel

3 Likes

OK, thanks very much.