how to import python modules from other files in the same directory in python node

Hi,
I created custom python node and I have an issue with import classes from other files (located under same directory as the python file of the node).
knime not reloading the custom node with the imports , with the error: “Failed to parse Python node extension at path”.
thanks in advance,

Hagay

Hi @hagay_finiti,

nice that you give Python node development a try!
The KNIME log has more information. Have a look!

Does this help already? Otherwise, could you send the KNIME log as well as the file containing the node and the file with the classes to be imported?

Best regards
Steffen

2 Likes

Hi Steffen,

thanks for your fast replay,

the errors I saw in the log were :

  1. “SyntaxError: invalid syntax match func[‘func’]:”
    match case works in python version 3.9 and higher - and should be valid.

  2. “ERROR : Worker-1: Workflow Coach loader : : RepositoryManager : : : Category ‘python-nodes’ from plugin ‘org.knime.python3.nodes.testing’ could not be created in parent path ‘/testing/’.
    java.lang.IllegalArgumentException: The segment ‘testing’ in path ‘testing/’ does not exist!”

  • dont know what this is about.

either way im attaching the knime log file.

thanks in advance,

Hagay

knime.log (305.3 KB)

problem was the match case - changing match case to if+elifs+else works.

I guess its not supported ,is there any way to make it work? maybe update python version in knime?

thanks in advance,

Hagay

Hi @hagay_finiti,

match case does not work with Python 3.9, it was introduced with 3.10 (What’s New In Python 3.10 — Python 3.11.4 documentation).

If you have a look at step 4 of the tutorial, and check your created environment, you will probably see that you use an environment with Python 3.9.

Also in this step 4, you see that three modules are specified for the installation: 1) knime-extension, 2) knime-python-base, 3) Python.
1)knime-extension brings in all the necessary API files such that you can use code-completion in your editor, if the environment is activated there.
2)knime-python-base is a metapackage which brings in dependencies like pyarrow and pandas etc, which are necessary for interacting with the KNIME Analytics Platform. If you look at Files :: Anaconda.org you see that we provide knime-python-base up to Python 3.11.
3)Python lets you specify the version. as you can see in 2), the version range made available by knime-python-base is 3.8-3.11.

This answers your two questions: there is a way to make it work, you have to create (and use) an environment which uses Python>=3.10.

conda create -n my_python_env python=3.10 knime-python-base knime-extension -c knime -c conda-forge

Best regards
Steffen

2 Likes

I admit: this is not clear from the documentation, I will adjust it and add a Trouble Shoot Section in some weeks.

Hi steffen,

thank you for your answer.

I got another question - (if its required another post please let me know):
I want to get some parameters as input from the user for my node

  1. choosing 2 columns names out of the columns the input table have ( not just regular text parameters )

  2. list of numbers lists, for example the : [[1,2],[3,4],[5,6]]
    I haven’t seen a way to make it happened yet.

thanks in advance,
Hagay

Hi @hagay_finiti,

  1. Simple: Use the column selection twice - tell me if it works for you!
  2. Less simple: Use a string and parse it (not very nice). If the need arises, we could think about adding a ListParameter.

(thanks to Benny for helping)

Does that help?

Best regards
Steffen

1 Like

Hi Steffen,

thank you and to Benny.

  1. using column selection twice works for me.
  2. does that mean that there isn’t ListParameter? (lets say I want to get list of strings out of enum that you can choose from - it must required using stringParameter multipule time?

-another problem I got is getting error on loading the node - parameter missing for key “target_col”
after I click ok and continue - the node works fine.

the parameter target_col :
target_col = knext.ColumnParameter(
label=“Target column”,
description=“Select the Target column”,
port_index=0,
column_filter= None,
include_row_key=False,
include_none_column=True,
)

thanks in advance,
Hagay

Hi @hagay_finiti,

correct, there is no ListParameter.

If you just recently added the target_col, that is desired: when reloading the node, it cannot have any settings for that parameter. It should not appear again. Does it?

Does this answer all your questions for now? :slight_smile:

Regards
Steffen

1 Like

Hi @steffen_KNIME,

thank you for your help, that’s answer all my questions.

Hagay

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