Execute failed: output_table_1 is not defined.

Hi,
I have a problem running python script on Knime 4.5
It successed when I used fileA to run python script, but it failed when I change to another fileB which have the same columns and same columns type. So the python script should work as same.
But it shows
“ERROR Python Script 0:120 Execute failed: output_table_1 is not defined.”

here is the output part of my coding.

if ‘NR_SITENO2’ in df.columns and ‘column2’ in df.columns:
print(“exist”)

# カラムを文字列型に変換
df['NR_SITENO2'] = df['NR_SITENO2'].astype(str)
df['column2'] = df['column2'].astype(str)

# Apply the function to each group
try:
    result_df = df.groupby(['NR_SITENO2', 'column2']).apply(assign_cell_names, reference_df=df2).reset_index(drop=True)
    output_table_1 = result_df
except Exception as e:
    print(f"Error applying groupby and function: {e}")

else:
print(“error”)

when I tried to execute script, “exist” will be printed in workspace, and it shows Execution successful.
But when I tried to execute the python script node, it shows failed.
ERROR Python Script 0:120 Execute failed: output_table_1 is not defined.

I already check this two files be inputed by CSV reader in different file has same column and same column type. Is there anything I am missing?
And it will be really helpful to know why it shows “output_table_1 is not defined.” when it actually be defined.

Than

Hi @Allison87,

welcome to the Knime Community. Without a screenshot is is difficult to assess the situation. Can you share one please? On another note, I found K-AI to be of great help in such situations:

Best
Mike

1 Like

Your code is not bulletproof:
if there is an error in the first line of your try block “result_df=…” , the except block is executed, leaving output_table_1 undefined.
Why there is no print out of the error message from the try block is strange. Can you change “except Exception as e:” to “except BaseException as e:” ?
For defining outpt_table_1 no matter if an error occurs:
You should set the output_table_1 to an empty dataframe before the try block: “output_table_1 = pd.DataFrame()” this way the “not defined error should” go away. This still does not explain why one file is ok, the other not.
For this you need the first line in your code to be: “import pandas as pd” with pandas installed in your python environment
I can not check in 4.5, but I assume that if you execute inside of the node the content of output_table_1 will not be evaluated, only if you execute the node, the error occurs because Knime receives no output from the node. In 4.7 even if you execute inside the node, an empty output will generate a warning/error.

Cheers