How to pass .csv as Input Parameter to Invoke Python Method Activity

Hello everyone,
I want to train a ML model using python script, I write that model into a function, then using Invoke Python Method Activity want to train that model. How can I gave those .csv file as an InputParameter?
Please help me to resolve this problem.

Thank You

Hi @Subhamay_Maity ,

In Invoke Python method, there is a property called InputParameters (which expects an IEnumerable type of value) -
image

You can create a variable of type [List<Object>] and use AddToCollection method to add your parameters into this list. Since [ List<T> ] implements the [ IEnumerable<T> ] interface, it is already an enumerable. Passing this listVariable in your InputParamters property of Invoke Python Method activity should do the trick for you

I am taking clues from this post - How to add strings to a ienumerable<System.String> - #5 by sarathi125

Thanks,
Nishant

Hello @Subhamay_Maity

Please check whether the below video can help you.

Hello, I had tried with a test Script.py file and a file Model.csv
In an empty new project, I had added the Python activity libraries.
Then from UIPath.Python.Activities I added a Python Scope activity.
At Library Path property of Python Scope I have the next setting:

string.Format(“{0}\AppData\Local\Programs\Python\Python37-32\python37.dll”,
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))

And for the Python Path, I have:

string.Format("{0}\AppData\Local\Programs\Python\Python37-32",
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))

For simplicity, I used a Set Variable action for the path to a csv file.
path = “./Model.csv”

Then I used a Load Python Script action.
The File property is set to “./Script.py”
Also for the Output Result I had declared an obj variable.
This obj variable is of type: “UIPath.Python.PythonObject”

Next I had added an Invoke Python Method
At Input Parameters I have a list of arguments like this: { path }
Where path is the file name for the “Model.csv” file.
Here comes that obj variable, that has to be placed at Instance.
For the Name of the function, I have a method called “read_model”.

This one I had defined into the “Script.py” file.
For editing the Python script, I had used an application called IDLE.
What really matters it’s the way, the arguments are being passed.
Here it is the code for this script file:

import sys
import csv
import os

def read_model(*args):
pad = args[0]
if (pad == “”): return
if (not os.path.exists(pad)): return
print(pad)
csvfile = open(pad)
reader = csv.reader(csvfile)
for line in reader:
print(line)
str = line
csvfile.close()
return

#read_model(“./Model.csv”)
#EOF

You can change this script file as you need.
At this moment it doesn’t give any errors and it seems it works.
For your understanding, this is the way the actions are being placed.
Please see the next image:

Hopefully, this would be of any help. I think, I learned a lot from this example.
With fine regards, Adrian Brinas.

I forgot to write, the code indentation in the script file, it’s not just correct.
It may give some errors, if there are not inserted some tabs spaces.
Regards, Adrian.

And the path is not a String, it is declared as Object.
I hope this is all. Adrian