Invoke Pythod Method Error

Hello,

I have a script whereby I read an excel with pandas as a Dataframe and then I write the excel again as shown below:

My only input parameter is the file name noted with the variable “file”.

When I run the script in PyCharm it works fine but when run in uiPath I have an Invoke Python Method: One or more errors occurred.

What shall I do? Thanks for the help!

@Yudhisteer_Chintaram1

Can you type the warning message from UiPath?!

Only this: Invoke Python Method: One or more errors occurred.

You can refer the following example…
PythonActivitiesTest.zip (2.5 KB)

For other function it is working properly. For example this code to square numbers. It runs in UiPath.

image

But to write an excel, this one is not working.

The output data-type you can change in the properties of “Get Python Object”, and then you can use write cell Activity.

No. The python object already write the excel. I just want to run the python file.

Hi @Yudhisteer_Chintaram1 , Could you place the first two Import lines inside the function and test it.
Also assuming that the file assignment is not being used when used in UiPath. If used, please delete the file assignment line as well.

Let us know if this doesn’t work or if you get a different kind of error.

Remove func(file) and try once…

Nopes. Doesn’t work.
Same kind of issue.

Still doesn’t work

Test to change your file_path variable. Remove r and replace with double backslashes. Or change them to forward slashes.

E.g.

file_path = "C:\\Test\\gym.xls"

or

file_path = "C:/Test/gym.xls"

Nopes. Neither solution works.

@Yudhisteer_Chintaram1 ,

Could you Provide us the Code Snippet in a Text File or use the Ctrl + E to add the Code Snippet in the Preformatted text.

Also, we do see the file path assignment has been deleted, but the imports are not moved inside the function. Have we tested it by moving the imports inside the function?

import pandas as pd
import numpy as np

def func(file):

    df = pd.read_excel(file)
    print(df.head)
    df.to_excel("output.xlsx",sheet_name='Country', index = False)
    x = "Good"
    return x

Yes. I tried with the imports inside the function and it still does not work.

Could you share your workflow also? I just tested your Python code on my computer and it’s working fine.

image

image

Extract_Country.xaml (7.9 KB)

Can you share yours please?

Of course. Here’s my workflow:

InvokePythonTest.xaml (6.6 KB)

On another note, have you tested to add the full path for the output file? I suspect that the script maybe is writing to a directory that you might not have write access to.

df.to_excel("c:/test/output.xlsx",sheet_name='Country', index = False)

Your workflow works fine on my computer also. The only difference I can see is that you are using another version of Python (3.8 vs 3.9).

image

image

image

Hi,
did you find the solution for this error ?