Passing arguments to Python

A quick question. When I run the below Python Code in command prompt, it throws me an error saying name is not defined. I am new to Python programming. I am at the beginner level learning basics of python programming. Can you please help? In the below code, where is the name defined and what is the text here? How does the if statement validates here? Sorry, if it is a basic level of question.

import sys

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

if name == “main”:
run(sys.argv[1])

Hi,
You were nearly there but name is __name__ and main is __main__

The double underscores are important before and after the text. Also make sure your indentation is correct after the if statement.

Hope this helps
Bob

Thanks Bob. I am trying to pass body of the email to Python code. Below is the workflow.

image

Below is the properties for Load Python.

image

Below is the properties for Invoke Python.

image

Below is the properties for Get Python

image

The variable definition in UiPath:

Below is the Python code.

#!/usr/bin/python

import sys

def print(name):
return "Hi " + name

if name == “main”:
print(sys.argv[1:])

When I run it in UiPath, I am getting below error:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: Error loading Python script ----> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Exception has been thrown by the target of an invocation.

Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object ins, Object outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at UiPath.Python.Service.IPythonService.LoadScript(String code)
at UiPath.Python.Impl.OutOfProcessEngine.<>c__DisplayClass11_0.b__0()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at UiPath.Python.Activities.LoadScript.d__12.MoveNext()
— End of inner ExceptionDetail stack trace —
at UiPath.Python.Activities.LoadScript.d__12.MoveNext()
— End of stack trace from previous location where exception was thrown —
at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

Can you please let me know where I am going wrong?

Hi

I’d suggest a couple of things

  1. Rename your function from print to something else as print is a built in python function itself

  2. Python is case sensitive and you’re calling Print when the python function is called print (lower case)

  3. Try to run the python script from the command line before trying from UiPath. It it runs then at least you know the python is valid

Regards
Bob

1 Like

Hi,

Please find attached my .xaml and the python code.

GetOutlookMessage.xaml (10.8 KB)

I tried executing it in JupyterLab and below is the screeshot.

image

Can you tell me what could be the issue? When I run this code using UiPath, it is throwing Load Python Script error. Your help is much appreciated.

Hi,

I tried your script and when I use the mail body it also throw errors, but it works if I change the argument to be the mail subject. My best guess is that the email subject contains unescaped quotes that are not being passed to the script correctly, or possible the mail body if longer than the allowed argument length in python (although I doubt this).

Here you can see it working with the mail subject
image

regards
Bob

I tried executing it by changing the body to subject, still i am getting the error. I am not sure if sys.argv is causing issue for me.

When I try to execute the simple print statement in the JupyterLab, I am getting below output instead of Learning Python sysargv.py.

You know that sys.argv[0] is always the name of the running script and sys.argv[1] is the first passed argument right?
I used this script in my test:

import sys

a="Learning Python"
def robot(a):
	result=str(a)
	print(result)
	return(result)
	
if __name__=="__main__":
	robot(sys.argv[1])

When I tried running, I am getting -f

image

I’m confused,how are you running it? If you run this from within an IDE without using any arguments then you won’t have any to show.
I recommend creating a python file with this script and then call it from the command line with arguments, like myscript.py “This is a test message”

Hi,

It is running now for me. Thanks for your patience in explaining this for me.

But when I try to call this python script in UiPath, it is throwing Load Python Script error.

Are you calling a python script or the Jupyter notebook from UiPath?
If you create a python file (.py) like the one I used and call it from the activity it will work as expected.

I am calling python script (Categories1.py) from UiPath.

It is working now. The version of the Python I am using is 2.7 but I gave it as 3.6 and mentioned it as 64 bit instead 32 bit. After correcting it, it is working fine.

I wanted to know how to pass generic value to the python code. In my .xaml file, I am passing SubjectMessage as an input parameter. But finally in the message box, instead of subject line, I am getting SubjectMessage displayed. I have attached the screenshot below. Can you please help.

image

Hi,

The above example you provided is for integer value. How about the variable of Generic variable type? How to pass this variable to python code via input parameter property of the invoke Python Method?

I would recommend you don’t use Generic types, in my experience string, integers and lists work OK but I’m not sure generic is supported.

With the SubjectMessage are you passing {“SubjectMessage”} by accident when it should be {SubjectMessage}. Seems you are passing a constant string rather than a variable.

Hi Bobpeers,

it is working fine for me. I am able to pass the subject as well as body to the python code and retrieve it back. Only thing is i need to assign this generic value to another variable of type string. Thanks for your patience as well explaining me in detail.

Now I am focusing on sending email content with another variables (2 variables as input) to html page.

Can you guide me on the activities what needs to be called as well the properties that needs to be taken care for those activities?

Hi Bobpeers,

The workflow is working fine for me. When I run the same workflow in a different system, I am ending up with Load Python Script error. I checked all the configurations. It looks the same as it is in my laptop. Can you help?

Regards,

Prameelaj

Hi Bob

Just curious, If it supports I Python Notebooks as well ?

Problem statement is passing an input string to an I Python Notebook which is running ML Algorithms under the hood to classify input variable and label string to a particular category. Output variable (Classified string) is subsequently triggering a UI Path Workflow. Thanks !

Hi,

you need to convert the notebook to a plain python file for it to work using nbconvert, https://nbconvert.readthedocs.io/en/stable/usage.html

Hello,

Is datatable able to be passed as argument ?

We need to use it with pandas ?

Regards,
Diogo Nunes