Python Activity : Invoke Python Method

Hello @Sukanya_Dutta
Refer this thread , it may helps you

This error usually occurs if are using python script and it has errors in paths, variables etc.

Method name is case sensitive, could you check if the same method name which you are trying to call is defined in the script.

Yes both are same

Have a look on this thread, this might can help you

Hi @Sukanya_Dutta,

Your Read File Path is set incorrectly

It should be Downloads\creditcard.csv
Not Downloads\creditcard.csv\creditcard.csv

So your python scope is not getting the correct input.

We cannot see your Python method code and what you return.

Hi @Sukanya_Dutta

Are you using any file path inside your code?
If it is so, try to make it absolute.

what is the type of object which is returned by the method used in the code.

import pandas as pd
import joblib
import json

class Main(object):
def init(self):
pass

def predict(self, X):
model = joblib.load(‘finalized_model.sav’)
X = json.loads(X)
result = model.predict_proba(X)
return json.dumps(result.tolist())

if name == ‘main’:
with open(‘./creditcard.csv’, ‘rb’) as input_file:
bytes = input_file.read()
m = Main()

Hi again,

We do not know where your inpur csv is. My suggestion was just by looking at your Read File Location. If you know that the input csv location is correct then ignore what I said before. We want to you eliminate causes for failures.

Please read this post to see how to properly use methods from your python scripts.

Using class methods is not the same as using functions. Here you have a class method and not a function. UiPath can execute functions not class methods.
Invoke Python Method: Pipe is broken - Help / Activities - UiPath Community Forum


I would have simiplified your code like this. I cannot test this so you will have to check that this works outside UiPath first.

You are not using Pandas here so, ignore it.
Make sure you are using absolute path in model = joblib.load(‘finalized_model.sav’)

import joblib
import json

def predict(in_argument):
    """ 
    This function predicts (Something)
    in_argument : Your input argument
    Returns : Predicted json string to UiPath
    """

    with open(‘./creditcard.csv’, ‘rb’) as input_file:
    bytes = input_file.read()

    model = joblib.load(‘finalized_model.sav’)
    payload = json.loads(in_argument)
    result = model.predict_proba(payload)
    return json.dumps(result.tolist())

I also suggest you to refer to PEP 8 – Style Guide for Python Code | peps.python.org

Code

  1. When you run in outside UiPath studio, does the python script work? It is worthwhile to check if it does before integrating with Python Scope in the Studio.

  2. Can you show the properties for the “Invoke Python Method” activity. We want to see how you have passed your input variable to the method “predict”

Hi @Sukanya_Dutta ,
If you create .py file and run directly by double click then you can use Start process activity to call .py file. This will work for sure. But only disadvantage is that you can’t utilize any of variables generated by python engine in side the UiPath code.
Alternatively you can save results set on a notepad in Python code and read Python output by UiPath activities and utilize them.

Hope i am clear in my explanation.

Hi again,

I think you should avoid reading the same creditcard.csv both in the python function and UiPath

Keep it simple and short.

import joblib
import json

def predict():
    """ 
    This function predicts (Something)
    in_argument : Your input argument
    Returns : Predicted json string to UiPath
    """

    with open(‘./creditcard.csv’, ‘rb’) as input_file:
    bytes = input_file.read()

    model = joblib.load(‘finalized_model.sav’)
    payload = json.loads(in_argument)
    result = model.predict_proba(payload)
    return json.dumps(result.tolist())
  1. Python Scope
  2. Load Python Script
  3. Invoke Python Method (without any parameters) for example you only have to call “predict”
    without any parameters
  4. Get Python Object

But without any input parameter How can the model predict the output ?

Why would you want to send the InputData as an array here? Why not just send it as a string? Let json.loads(X) do the searlization in the python script.

All along we got an understand that your csv file has all the input and output data and probably your pipeline does the inference. We do not know what your joblib pipeline looks like. What you are trying to achieve and what is the output you are looking for. I think these are very important. Without a proper question, it is next to impossible to debug such problems remotely.

How to ask a good question? - News / Tutorials - UiPath Community Forum

hi, my problem solved by install openpyxl. open cmd, type “pip install openpyxl”. Then run again and SUCCESS!!