Importing from Python py file

Greetings. I am currently developing some solution and would like to integrate my readily build python .py files to Knime so that I can just call the function from my .py and get the result in the workflow.

The question would be, how to import scripts that we have created as py on knime? Is it doable?
Where should I put the py files?
Can someone please give me some sample?

Thank you in advance,
sorry for asking too much.
Best regards,

Hi Mizu,

Yes, this is possible (but maybe not that convenient).
How it works:

  • Place the Python files somewhere inside the directory of your workflow which is located in your workspace directory. This ensures that the script also is exported if you export the workflow.
  • Get the path to your workflow via the Extract Context Properties node (this makes your workflow portable).
  • Pass the path flow variable to some Python node (which come with our Python integration).
  • Inside the node, add the workflow path to your sys.path and load the files just like any other Python module (alternatively, use importlib.util, see here).

I attached (7.5 KB) a tiny example workflow where I did just that.


1 Like

Thanks… I should have realized that it should be put under the same workflow directory.
Best regards,

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