Running Python Script in UiPath

Hi All, @loginerror

I have a problem which I am facing that I cant solve. I have a python script that converts PDF into CSV files. When I run the script in Spyder, it works perfectly.

I want to leverage on the latest [https://docs.uipath.com/releasenotes/docs/uipath-python-activities](https://Python v1.3) and use UiPath to run this script run. However, I am unable to do so as I am being met with Invoke Python Method: One or more errors occurred.

Basically, within a Python Scope, I am using ‘Load Python Script’ (No error) > Followed by Invoke Python Script (Error occurs here). I also tried using the ‘Run Python Script’.

I have attached the python script here for the community’s easy reference.

pdf2csv.txt (1.2 KB)

Thanks in advance! :grinning:

======================== Update on 16 Feb ====================

Hi all, @burque505 , @SenzoD , @NIVED_NAMBIAR , @loginerror , @Nalin

I have simplified the script and added screenshot to illustrate better. Essentially once I comment out the targetPath = filedialog.askopenfilename... statement, it will work (else, error is thrown at Invoke Python Method). Any idea why ? it seems like uipath was unable to read the filedialog methods to me.

As communicated, the script works perfectly if i run it via cmd / Spyder => filedialog statement will prompt user to select file to upload.

Hence, not sure why uipath is throwing me an error.

Thanks in advance! Any comments are welcomed!

Jack

Have you seen this yet?

Hi @robot_learner

Which python version u are using?

Hi @SenzoD ,

Yup, i saw it, to share, for simple python files, they work (i.e. no errors). But I realised for instance in my case, If i remove all the codes in my script less the import statements, they will work. However, when I add in the details such as the below, the error will come.

    root = tk.Tk()
    root.withdraw()
    targetPath = filedialog.askopenfilename(title="Select the pdf file to convert!") 
    outputPath = filedialog.askdirectory(title="Select location you wish to save output result to!")

My objective is to basically just run the python file using UiPath. The python file which I shared can be tested too and it works fine on Spyder / running via command prompt.

Thanks!

Hi @NIVED_NAMBIAR ,

My machine is using Python 3.7. The UiPath package am using is UiPath.Python.Activities v1.3.0.

Thanks!

I think there is some changes too u had to make in code like adding import sys module

Can u do one thing debug the flow and see the complete exception details

And put a screenshot here so that we can understand here what error is there actually

Hi @NIVED_NAMBIAR ,

Here you go :slight_smile:

My python script written inside expression editor of the Load Python Script Activity

"
def hello():

	import tabula as tab
	from tkinter import filedialog
	from tkinter import messagebox
	from datetime import datetime
	
	#the below statement will throw an error in UiPath. It works when I run it via cmd / spyder
	#what it does is that it will prompt user to select file to upload.

	targetPath = filedialog.askopenfilename(title='Select the pdf file to convert!') 
	return 'Hi'
"

Logs

   19.10.4+Branch.support-v2019.10.Sha.fe819a658cad0585a64d2f8af94fad5c30b8d122

 

Source: Invoke Python Method

 

Message: One or more errors occurred.

 

Exception Type: System.AggregateException

 

RemoteException wrapping System.AggregateException: One or more errors occurred.  ---> RemoteException wrapping System.InvalidOperationException: Error invoking Python method  ---> RemoteException wrapping System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]: IndexError : list index out of range

 

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.InvokeMethod(Guid instance, String method, IEnumerable`1 args)

   at UiPath.Python.Impl.OutOfProcessEngine.<>c__DisplayClass14_0.<InvokeMethod>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.InvokeMethod.<ExecuteAsync>d__16.MoveNext()

                --- End of inner exception stack trace ---

   at UiPath.Python.Activities.InvokeMethod.<ExecuteAsync>d__16.MoveNext()

                --- End of inner exception stack trace ---

   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)

Hi @robot_learner

Could you please mention your version of the Python activity package?

Hi @loginerror

My machine is installed with Python 3.7 (64 bits).

The package am using is UiPath.Python.Activities v1.3.0

Essentially, I am trying to run a python script in UiPath. I have placed a ‘Run Python Script’ within a ‘Python Scope’ activity. In the ‘Run Python Script’, I have placed the following simplified code which uses advance features of python.

import tabula as tab
from tkinter import filedialog
from tkinter import messagebox
from datetime import datetime

#the below statement will throw an error in UiPath. It works when I run it via cmd / spyder
#what it does is that it will prompt user to select file to upload.

targetPath = filedialog.askopenfilename(title='Select the pdf file to convert!')

Thanks in advance! :slight_smile:

Hi @robot_learner

An long overdue update. Could you have a look at this post for some insights around this issue?