Use PYTHONPATH in KNIME-Server

If I want to reuse self written Python scripts I have to add a line like this to each Python node on my KNIME server system on Ubuntu Linux:

sys.path.append(‘/path/to/my/scripts’)

I wondered if I get avoid this extra line. I tried to set a PHYTONPATH in /home/knime_user/.bachrc

export PYTHONPATH=${PYTHONPATH}:/path/to/my/scripts

but in the webportal the scripts cannot by found. Is there a solution for setting the pythonpath that is used by KNIME?

A workaround is to put the files in the “site-packages” folder of the Python environment since this folder is in the pythonpath in KNIME but I would like to have a better solution.

Hi @spider,

your right, putting the scripts in the “sites-packages” folder would work, but its not the most elegant solution.

Another thing that could work and is similarly non-elegant would be to put the scripts into the workflow folder, which is the also the working directory of python. This is nice as it makes your workflow portable, but I think you aim at storing the scripts rather more centrally, right?

A way for you could be to use a startup script to set the path there before python starts. This would be the “manual” option in the preferences page, an example can be found in the docs, under “Option 2: Manual”, the second choice.

It might even work to put the start script (or the export PYTHONPATH=${PYTHONPATH}:/path/to/my/scripts) in the .bashrc of the machine of the executor, since the start script is executed via bash anyways - but you’d need to try that, I haven’t tested.

Hope that helps!
Best
Lukas

1 Like

Hi Lukas,
I already tried the .bashrc way with no success (see my first post).
But after a second glance at the pythonpath output in KNIME I saw that the “workflow_directory” (the parent folder of the actual workflows) is in the python path. So I put my python files to this place and removed them from the “site-packages” and it works as expected.

1 Like

Ah, somehow I misread that and suggested exactly what you already tried :sweat_smile: Sorry for that, I’m glad you found a solution!

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