Portable Python Components

Hello KNIME Community,

TLDR: Is there a way to embed python module(s) in a component and reference them from the Python Script node?

I recently read this article. It was a great introduction but I don’t understand how the component is portable if the Python Script node is referring to a module that lives inside the workflow and not inside the component itself.

It seems that when you execute the Python Script node the current working directory is always the workflow directory and not the directory of the current component. Any modules you import into the Python Script node would then have to be placed in the workflow directory and would not exist if someone were to pull the component from the knime hub etc.

You could put the script in the component directory and use extract context properties to get the absolute path to the workflow directory and then add the component directory to that and add this to sys.path. This isn’t very robust though because the way knime names nodes you aren’t always going to be sure what the node ID for the current component is. If you choose to match the name regardless of the node ID it wouldn’t hold up to you having multiple components in the same workflow. Even if there was a way to make this work it would not be overcome by the issue below.

I have also tried sharing a component to my local workspace then adding the module inside of this component folder and editing the component to import the module. This all works fine when editing and testing the component but when I drag the shared component into a new workflow the module file does not come with it. It is the same when sharing a component. If the component has files in it knime does not recognize they will not be shared to your local workspace / knime hub.

I am hoping there is something obvious I am missing. I was assuming that components should operate like workflows where they are essentially containers that can be moved around and deployed with their dependencies all contained inside of them but that does not seem to be the case right now.

Any help or pointing me in the right direction would be much appreciated!! Thank you for your time!

Kaegan