cancel
Showing results for 
Search instead for 
Did you mean: 

C# code to run python script using arguments and get output into data item stage in object studio

VishalSalvankar
Level 2
Hi,

I am new to Blue Prism and I want to run a python script from coding stage using C# by passing arguments and storing its output in the data variable.

Please elaborate the code

Thanks in advance
- Vishal Salvankar

------------------------------
Vishal Salvankar
------------------------------
8 REPLIES 8

david.l.morris
Level 14
@ewilson wrote a great article on Code Stages and Python here: https://digitalexchange.blueprism.com/site/global/developer/code_stages/index.gsp


------------------------------
Dave Morris
3Ci @ Southern Company
Atlanta, GA
------------------------------
Dave Morris 3Ci at Southern Company Atlanta, GA

VishalSalvankar
Level 2
Hi,

I found the solution to call the python script using C# code

string result="";string result="";string ErrorMsg="";bool isError=true;try{ProcessStartInfo start = new ProcessStartInfo(); start.FileName = Python_Executable_File_Path; start.Arguments = Script_Path; start.UseShellExecute = false; start.RedirectStandardInput = true; start.RedirectStandardOutput = true; using(Process process = Process.Start(start)) { using (StreamWriter sw = process.StandardInput)     { sw.WriteLine(Input); sw.Close();     }
     using(StreamReader reader = process.StandardOutput)     {          result = reader.ReadToEnd();                } }  } catch (Exception exp) {    ErrorMsg=exp.Message;    isError=false;}
Exception=ErrorMsg;Success=isError;            ScriptOutput= result;

The above code needs to be used in the code stage in Object Studio.

The input and output parameters need to be given accordingly.

Do not forget to include namespaces in the Initialization page of the Object

Thanks

------------------------------
Vishal Salvankar
------------------------------

ewilson
Staff
Staff
Hi Vishal,

I see you've already figured out some C# to get you what you need. I did want to point out that there is now a VBO available on the DX that can assist with executing Python scripts. You can find it here:

https://digitalexchange.blueprism.com/dx/entry/9648/solution/blue-prism---script-execution-vbo

cheers,

------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

Hi Eric,

I tried running the python script with the Environment VBO. However, I keep getting an error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf3 in position 50: invalid continuation byte when running the python script. But when I run the script via CMD,i.e outside Blueprism, it runs successfully.

Please I need help resolving this issue.

Thanks.

------------------------------
Shakirat Jimoh
programmer
Waje smart solution
Africa/Lagos
------------------------------

@Shakirat Jimoh can you post a screen shot of your settings for the VBO?​

------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

212.png

Hi Eric 

Find above the image for the VBO Settings.


------------------------------
Shakirat Jimoh
programmer
Waje smart solution
Africa/Lagos
------------------------------

Just to be clear, you're trying to run a Python script using the Utility - Environment VBO, correct? What I see in the screenshot is an executable file that you're trying to call with some parameters, so you're executing something like this:

C:\Users\blueprism\Documents\GT_Solution.exe "C:\SomeFilePath" -f​

Typically, executing a Python script on Windows involves a reference to the Python interpreter which would look something like this:

C:\python3 HelloWorld.py [Parameters]


There is an actual Python execution VBO available on the DX. Have you tried that as opposed to the Utility - Environment VBO?

One thing I would recommend is to change your parameter data to something like this:

Chr(34) & [Destination FilePath] & Chr(34) & " -f"


I don't think this change will impact the error you're seeing, but it will make you're data easier to read and maintain.

Cheers,



------------------------------
Eric Wilson
Director, Partner Integrations for Digital Exchange
Blue Prism
------------------------------

@Shakirat Jimoh - Use VBO  - https://digitalexchange.blueprism.com/dx/entry/9648/solution/blue-prism---script-execution-vbo and run .py file directly with this. I have used it and it works great. Just ensure your VBO points to right python virtual environment with dependencies ​installed that are referred in .py file.

------------------------------
Mayank Goyal
------------------------------