Passing arguments to Python

Hello,

Im trying to use Invoke python Method activity with input arguments but its not working.

If i try to use this activity with out input arguments everything works fine.

Unfortunately when i try to pass string argument into it in form of List(of string) invoke method fails to run.

Activity fails even if argument isnt used in script

5 Likes

Can you send me a sample xml to properly understand what you are trying to achieve?

Ok i resolved the issue.

Turns out Python code cant use sys.argv if its to cooperate with UiPath.

Depends what you mean by using sys.argv. I can call this script from the command line with an arguement or call the run() method directly from UiPath with the text argument. This is the python code:

import sys

def run(text):
    return " Hi " + text

if __name__ == "__main__":
    run(sys.argv[1])

and this is the invoke python activity to call the script:

image

The output is as expected, “Hi Bob”:
image

5 Likes

hi,
Uipath says that it does’nt recognizes sys.argv. The error is thrown at ‘Load Python Script’

Do help me out.

Does your python script start with the line import sys?

Can you run the python from the command line with a text argument? If it doesn’t run from the command line either then you must be missing the sys module (which is part of a standard install)

Hi,
Thank you for the quick reply. i was missing on the ‘if’ condition before the function cal. it worked!!

i still cannot understand the significance of the if condition there?

Thank you.

Hi,
Can you please tell me how do i pass multiple arguments in the Input Parameters section.
my arguments are variables.(2 file paths and 1 integer value).

Thank you.

For UiPath the if part is actually not necessary it would run just the same without it. But running from the command line it’s used to call the function.

To add more arguments simply change the python to be:
import sys

def run(arg1,arg2,arg3):
    return " Hi " + arg1

and call from UiPath using an array like {“Bob”,1,2}

Hi,
Thank you for the reply, I was wondering if we could pass a variable in the Parameters section of Invoke method.
for ex: in your example, “Bob” be stored in a variable in uipath and that variable be called in the parameters section.

I however did try passing a variable, the dropdown variable box appeared but evrytime i run the Workflow it throws an error saying “Error during Invoking the method”.

Hello,

How to do
“Hi Bob”
“Hi Mom”
“Hi Apple”

?

seems like the input parameter only allow one set of input??

image

Have you tried passing in a list variable instead?

You definitely can use variables in the arguments collection. If it doesn’t work for you then there must be some other error in your script or workflow.

Hi,
Capture1 Did try this but it returns an error saying that Invoke Method Activity has an error.

Capture2 This would return the string withing the double quotes.

This is a very basic python code of adding operators!

Hi Raman,

I just made this robot:

and the arguments are these:

image

the python code looks ike this:

import sys

def robot(a,b,c):
    result = int(a)+int(b)+int(c)
    print(result)
    return(result)

if __name__ == "__main__":
    robot(sys.argv[1],sys.argv[2],sys.argv[3])

And the robot print 124 as the result so it works as expected. Are you sure you are passing the input parameters as the type the python script accepts. For example are they type int32 or Generic, they need to be int32 for this to work.

BobPeers,
How did you get pyScript to have a method called run. In other words how did you load the function run enable it to be detected by UiPath?

Thanks,
Sam

Hi Sam,

when you load the python script in UiPath any functions (defined by the def statement) become available to call. You simply make your functions or methods in python and call them whatever you want and then call them from UiPath.
A python script could have 5 functions called def func1, def func2, def func3 and then you could call “func1”, “func2” or “func3” from UipPath.

Hello,
Thank you for your continuous guidance, I’m now understanding where the problem was at.
I did try the exact same code but i took the inputs from the use using the ‘input Dialog’ n then passed those to the ‘invoke Method’ activity. it throws an error i haven’t seen before.

er

and the exact same code n workflow throws this…

err