Python Activity: Get Python Object Issue

Hi There,

I’m trying to use Python Activities in Studio to execute a Python file(which connects to Mongo & fetches the output). Tried the conventional way of giving a Start Process - to open mongo, manually using click activities, type into, etc & then using get text to scrape data.
Now I’m trying to modify this workflow, wherein it calls a Python Script in the background & that handles DB connection & execution just like any other programming language & returns the output.

Workflow → Python Scope(Using Python3.5) → Load Python Script → Get Python Object → WriteLine

Scope activity & load script seem to be working just fine. But Get Python Object is erroneous:
“Error converting the Python Object”
Tried using the debug mode, breakpoints, etc. The output from Get Python Object is always NULL.
It doesn’t seem to be converting the Python Object to a String or Int32.

Attaching the error herein.
Please Help :cry:

Hi @sandhya.dev01, is your python function returning a String variable?

@quihan
Hey, Well… My DB use case returns 2 integer values. But i changed this file before posting the screenshot. The problem is, even a Hello World string isnt getting displayed :frowning:

Hi @sandhya.dev01, have you tried running the demo workflow?

Python_Mini_Demo.zip (2.5 KB)

@quihan Yes, i think i tried this… didnt execute for some other error. wasn’t related. but mine is pretty simple. Just a python script printing “Hello” & still the get python object error persists. The other script i have is the actual workflow including Mongo as well. But here, it’s stuck just at a basic statement itself

@sandhya.dev01 that demo contains a simple “Hello {name}” function too. May I know what’s the error when you are running the demo workflow?

@quihan, you seem to have used “Invoke Method” activity right, to pass arguments. In any of my python scripts, we dont need to pass any arguments.
Also, your script kept giving error for Py 3.6.
Since i had error with 3.7 version, I even tried using Py3.5 but it still shows issues.
Shall i attach the workflow here?

sure @sandhya.dev01, you can attach your workflow and the script you are trying to invoke.

Cheers.

1 Like

@quihan
here, i have attached the workflow and scripts.

Note: tried using Python PythonProject.zip (3.3 KB)
3.5 & Python 3.7

hi @Ruchi15, based on my understanding, if you use Get Python Object, it will expect for an .net object. This means it can only be used after Invoke Python Method which will return some object. If you are trying to run a script, Run Python Script works fine. Besides, the output of Load Python Script activity is a instance to be used for Invoke Python Method.

I added a “Hi_Test.py” that print “hi” via a message box.
PythonProject.zip (3.8 KB)

**Important note: It only works with 32 bit python. Mine didn’t work with 64 bit

Cheers.

Hey @quihan, Thank you. I’ll try this approach too. but here in this project also… we never used Invoke method activity. Well, i assumed there was no need to pass arguments so i didnt use it.
Also, about the Run Python Script activity – this doesn’t seem to give any errors but i dont know whats happening as in, it doesnt show the output right?

Well, i did check your workflow & figured what Run Script activity would do. however, i’m still not clear as to how would I use Load/Invoke activity together… the out needs to be returned to UiPath again

Also, files we attached had both Mongo use case as well as a simple straightforward Hello. We used Python 32 bit. Both versions 3.5, 3.7 but it didnt seem to work.

Hi @sandhya.dev01, Load Python Script returns handlers used for Invoke Python Method to run any function declared in your script.

Whereas, Invoke Python Method returns a Python Object that can be further processed using the Get Python Object and convert to a .Net datatype.

If you just use Run Python Script, it should be able to establish a connection. Instead of print(), use the message box so that you can see your session ids.

Cheers.

1 Like

Hey @quihan, well… we tried using Invoke Python Method but it still ends up giving issues. There cant be mongo connectivity issues right? Also, we had no method in the code. It was just direct code statements mentioned in “Hello.py”. I dont understand why there issues coming up again n again

Hi @sandhya.dev01, your error is at Get Python Object ? If there’s no method, you don’t have to use invoke python method

@quihan hehehe, well then we’re back to square 1 right…? how do i get this working then. Did you happen to check the Hello.py file that i attached here? It has no methods, & i dont want the output in a message window. it has to be returned in UiPath as a variable so that i can use it further.

@sandhya.dev01, if you want it to be returned to UiPath, then we have to modify your script. What is your expected returned value?

@quihan, Thank You :slight_smile:
Well, alright… its an example use-case & My script connects to Mongo & does the following:

  1. There is data in a collection called “UserSession” & there’s multiple documents like these:
    {
    “_id” : ObjectId(“5b59b7ce1f44538037784e6d”),
    “URL” : “https://docs.mongodb.com/manual/mongo/”,
    “Session_Id” : “1236544hhhj”,
    “Cookie_Id” : “411lok”,
    “Previous_URL” : “https://www.google.com/”,
    “createdAt” : ISODate(“2018-08-07T12:00:14.735+0000”)
    }

So for the first case, the query –
resultCount = len(collection.distinct(“Session_Id”))
print(resultCount)
needs to give me a count of unique IDs & it’ll return a number which i want returned in UiPath.

  1. The next use-case is to retrieve the count of records where the field “Previous_URL” has the pattern google & this data is to be fetched from yesterday’s date… all that is implemented. So second case gives me another count in the database.

These are 2 expected integer values i need :slight_smile:
Please do let me know if any other detail is needed too.

I have used the Python_mini_Demo - my question is can I pass numerous variables into the method?

Thanks!

K

yes you can pass multiple variables