How to initialize python object ?
Hi, I had tried myself the next sequence of Python activities:
Please see the next picture:
In the Python Scope, the first activity is Load Python Script.
I have a Python file, which has a relative path: “./Script.py”.
This is the argument File of the Load Python Script activity.
The output from Load Python Script can be a new variable obj
The type of this obj should be “UIPath.Python.Python Object”
In this script, the input argument is reffered as: args[0]
Actually it is a path string to a file, that I want to read.
If I use args[1] for one single argument, I can get an error.
This file has been edited with IDLE 3.7 (32 bit).
After Load Python Script I used Invoke Python Method activity.
As input arguments I have a list with just one string item:
{ path }
The Instance is the obj variable about I had written before.
Name is the method I want to call. I have a method read_file
At Output, it is also used the obj variable for the Result.
The last one after Invoke is the Get Python Object activity.
Here is the part, where I also got several unhandled errors.
It was about conversion errors of Python obj to another type.
Obviously the input object is the PythonObject named as obj.
The TypeArgument is preselected as a generic System.Object
And for the Output I have a new Result variable named result.
If I declare result as an Object, everything it works fine.
When I use a String, or an Integer, it throws an exception.
Using a wrong type, could result to something like an error.
So, if I need a String, I converted this result to a String.
Unlike other methods, a generic type Object can be accepted.
This is because Object is the root for all other NET objects.
And a conversion from System.Object to another type can work.
Hope this can be of any help, Adrian.
@Adrian_Brinas I have encountered a similar issue (Get Python Object: Error converting Python object). My python script can return either a string output or JSON output. In the “Get Python Object” method, I declared the output variable as Object and I am getting an error. I am getting this error, irrespective of the data type of the variable. Appreciate it, if you could pls help me.
Hello, please excuse me for my late answer. I had uninstalled my UiPath Studio program and I had troubles with over-warming on my old computer. Also in the last time I was more sic and I had missed a lot of my last replies on the Forum’s community. And now I’m doing something else, I have some architectural plans to be drawn in Floorplanner
As I remember, the Get Python Object widget has some input and output variables, that can be declared as arguments, or returning value for a Python script. These variables can be used anywhere in the script, by the difference to a classic Python script, where it has to be declared the self-executing function main(), that uses a different method of extracting the input variables from an array of arguments of the main() function.
As a first tip I can say, you should check first if any of the input variables are Null, or Empty, or None, that is: the entry variable has not been initialized with a valid value. In this case, an error could be thrown, so better return an output value, than calling a method on an uninitialized object.
if (input is None) :
return output
Then you can print into the Debug window the name of an object type being used for input
print(type(input))
The returning value should be just 1 type, cannot have different types, from this reason you had tried to use a variable of a generic type Object for the returning variable. Just an idea, maybe it has to be used a ‘System.Object’ instead of simply an ‘Object’. Meaning that sometimes can be used a fully qualified name of a NET.FRAMEWORK object class. I don’t know exactly, to which NET class is more compatible with the JSON object, you must see the full map of classes.
So a type checking should be necessary. And use an if statement, to make a comparation.
if (type(input) == type(output)) :
output = input
else :
output = input.GetJSON()
return output
Here you should call the appropriate method you use for a certain object type.
So it must be called the right method for the proper object type.
But please take care, that a generic object type can return the same type name. However it can’t be made the clear difference between 2 different object types, that are encapsulated into a generic object class. Sometimes can be used for comparation a class name instead the object type, or the base class name of a variable object.
For more information about type checking and inheritance of a generic object class in Python, you can search Google after ‘type checking in Python’. Also an AI Companion robot, can answer to the problem with the most common examples and useful links to related subjects, or even from the UI-Path Community.
Hopefully I had found several clues, that can help solving the problem.
With kind regards, Adrian.