Python scripting

How do I pause the execution of a python script in the Python Scripting node?
I have tried using input(), and the program continues executing and does not stop by hitting a key.

Hello @Bob_Nisbet
Cancel all running nodes (Shift+F9) ribbon’s button ?
Just guessing.


No, there are no other nodes executing at the time.
I just need to pause execution temporarily until I can read my debugging print statement. There are ways of doing this in other languages like C, Perl, Fortran, etc. I want to know how to do it in Python through the KNIME Python API. Input() doesn’t do it. Python documentation says that input() should work; but it doesn’t work in the KNIME Python Scripting node.

@Bob_Nisbet I do not think it is possible to temporary halt a Python Script node and look at the input. What you can do is log the messages and also print() to a log file and take a look at that later:

Here is the ‘pure’ code. You would have to adapt the path.

import logging
import sys
import datetime

class StreamToLogger:

    def __init__(self, logger, level=logging.INFO):
        self.logger = logger
        self.level = level

    def write(self, message):
        if message.rstrip() != "":

    def flush(self):

    def _log_without_recursion(self, message):
        original_stdout = sys.stdout
        original_stderr = sys.stderr
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

            self.logger.log(self.level, message)
            sys.stdout = original_stdout
            sys.stderr = original_stderr

def stop_logging():
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__

timestamp ="%Y-%m-%d_%H-%M-%S")

# adapt this path to your needs (obviously)
logfile_path = "/Users/m_lauber/Dropbox/knime-workspace/hub/kn_example_python_log_to_file/data/"

# Create a filename with the timestamp
logfile = f"{logfile_path}logfile_{timestamp}.txt"

logger = logging.getLogger("my_logger")

# Create file handler and add it to logger
fh = logging.FileHandler(logfile, mode='w')
fh.setFormatter(logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S"))

# Logging levels provided by Python's logging library are:
# logging.DEBUG: Detailed information, typically of interest only when diagnosing problems.
# logging.INFO: Confirmation that things are working as expected.
# logging.WARNING: An indication that something unexpected happened or indicative of some problem in the near future (e.g., 'disk space low'). The software is still working as expected.
# logging.ERROR: Due to a more serious problem, the software has not been able to perform some function.
# logging.CRITICAL: A very serious error, indicating that the program itself may be unable to continue running.

# Redirect stdout and stderr to logger
sys.stdout = StreamToLogger(logger, logging.DEBUG)
sys.stderr = StreamToLogger(logger, logging.DEBUG)

# ---- you Python code goes here 

logger.debug("This is a debug message.")"This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

print("Print this message")

x = 2 / 1


# ----- Stop logging to the file


Thanks for the very detailed response. I suspected that it can’t be done via the KNIME Scripting API. Your work-around logging script is very helpful. I will use that facility.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.