Python Script

The node allows executing a Python script in a local Python 3 environment. The environment has to be configured under File → Preferences → KNIME → Python or via flow variable, as described in the KNIME Python Integration Guide. Multiple input and output ports of various types can dynamically be added to the node and used in the script. See sections Ports and Editor below for a description of all available inputs and outputs, and how they can be accessed in the Python script. Furthermore, the node allows to import Jupyter notebooks as Python modules via the knime.scripting.jupyter module that can be imported in the script. See the API Documentation for a detailed description of the full API.

The node brings significant performance improvements over the nodes of the legacy KNIME Python Integration and enables working with larger-than-memory data. More details on how to transition to the node's new scripting API can be found here. There are also example workflows that further illustrate its use here as well as example templates on the Templates tab of the node.

Please consider the following prerequisites before using this node:

  • The packages py4j and pyarrow, are required to be installed in your Python 3 environment in specific versions. We recommend to create a new Conda environment that contains these packages via File → Preferences → KNIME → Python.
  • Columnar Backend should be enabled for best performance.

Options

Script

Editor
The editor allows to configure the Python script that will later be run when this node is executed. In the script, use the knime.scripting.io module (imported as knio by default) to access the node's input data and populate its output data as described in the Ports section below. Use the knio.flow_variables dictionary to access input flow variables by name and to add new output flow variables by name.

Execute script and Execute selected lines allow to execute the script, or selected parts of it, right in the dialog without having to execute the entire node. The outputs of an execution can be inspected via the workspace on the right side of the editor. Please note that all executions in the dialog share and reuse the same workspace. That is, outputs of previous executions are kept in the workspace where they are visible to later executions. You can use Reset workspace to clear the workspace before an execution if you want to make it independent of all previous executions. Executions of the entire node, outside of the dialog, are always independent of one another.

The editor optionally provides autocomplete (by pressing Ctrl + Space) if the Python module jedi is installed in the Python environment used by the node (default if Python environment is created through KNIME).
Workspace
The pane on the right side of the editor panel is the workspace and lists the currently defined identifiers in the running Python instance. Reset workspace removes the outputs of all previous script executions.
Console
The console below the editor panel displays the standard output and standard error of the running Python instance as well as other information.

Executable Selection

Conda environment propagation (Python 3)
Allows to choose whether to obtain the Python 3 executable from the KNIME Preferences or from flow variable.
  • Use KNIME preferences: use the Python 3 executable configured under File → Preferences → KNIME → Python to execute the node.
  • Use Conda flow variable: use the Conda environment propagated by the selected Conda flow variable to execute the node. The propagated environment must be a Python 3 environment.

Templates

Create from editor...
Clicking on this button creates a new template based on the script in the editor panel of the Script tab.
Remove selected
Clicking on this button removes the selected template. Built-in templates cannot be removed.
Apply selected
Replaces the script in the editor panel of the Script tab by the selected template.

Flow Variables

script
Controls the Python script executed by this node.
python3_command
Controls which Python 3 executable is employed by this node. The flow variable accepts the path to a Python executable, start script, or Conda environment as described in the KNIME Python Integration Installation Guide .

Input Ports

Icon
No description for this port available.
Icon
An input object. Input objects are automatically unpickled for you. You can access them via knio.input_objects. For example, the first input object can be accessed like this: knio.input_objects[0].
Icon
An input table. You can access input tables via knio.input_tables. For example, the first input table can be accessed like this: knio.input_tables[0]. Each table is an instance of type knio.Table. Before being able to work with the table, you have to convert it into, for example, a pandas.DataFrame or a pyarrow.Table. Please refer to the KNIME Python Integration Guide to learn how to do this, or take a look at one of the templates on the Templates tab of the node.

Output Ports

Icon
No description for this port available.
Icon
An output table. The table has to be populated via knio.output_tables in your script. For example, to populate the first output table, assign it a value like this: knio.ouput_tables[0] = <value>. The assigned value must be an instance of type knio.Table which can be created from, for example, a pandas.DataFrame or a pyarrow.Table. Please refer to the KNIME Python Integration Guide to learn how to do this, or take a look at one of the templates on the Templates tab of the node.
Icon
An output image. The image has to be populated via knio.output_images in your script. For example, to populate the first output image, assign it a value like this knio.output_images[0] = <value>. The assigned value must be a bytes-like object encoding a PNG or SVG image.
Icon
An output object. The object has to be populated via knio.output_objects. For example, to populate the first output object, assign it a value like this: knio.output_objects[0] = <value>. The assigned value can be of any type that can be pickled. It will automatically be pickled for you.

Views

Standard output
The output sent by Python to its standard output during the execution of this node.
Error output
The output sent by Python to its error output during the execution of this node.

Workflows

Links

Developers

You want to see the source code for this node? Click the following button and we’ll use our super-powers to find it for you.