Hi All
Thanks for the suggestions.
I wrote a small C# script using Process.start, which takes inputs as Python interpreter, Python script to execute and then arguments if any.
The Python script also need to be written in a supporting way to have stdout and stderr.
C# code :
// e.g. start.FileName = "C:\\Python27\\python.exe";
// Argument 0 = Path to your python script (example : "C:\\add_them.py")
// Argument 1 = first arguement taken from C#'s main method's args variable
// Argument 2 = second argument.... and so on...
//
// pass these to your Arguments property of your ProcessStartInfo instance
// In the Python script sys.argv[0] will contain the script name.
// ******************************************************
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = Python_Executable_Path;
start.Arguments = Python_Script_Path + " " + Python_Arguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput,
reader_err = process.StandardError )
{
string error = reader_err.ReadToEnd();
Error_Output = error;
string result = reader.ReadToEnd();
JSON_Output = result;
}
}
Sample Python :
## Write Print/stdout.write statements only when necessary
## Write Print/stdout.write statements only when necessary
## Avoid intermediate prints used for debugging in final version
## Otherwise it will be captured as output along with JSON
## sys.argv[0] contains script name
## import sys and json lib
/* ================ Sample Python Script ====================
import sys
import json
cnt = len(sys.argv) - 1
#print("Arguments received: {}".format(cnt)) ==>> AVOID THIS!!
sum1 = 0
if (cnt < 1 😞
sys.stderr.write("No arguments were passed..!!")
sys.stderr.flush()
sys.exit(0)
else:
j = 1
for i in sys.argv:
if (j > 1): # skip the first argument
sum1 = sum1 + int(i)
j = j + 1
outdict = {'Name':'John', 'Grade':'Consultant', 'Param cnt':cnt, 'Points':sum1}
json_string = json.dumps(outdict)
sys.stdout.write(json_string)
sys.stdout.flush()
sys.exit(0)
==================================================== */
------------------------------
Nikhil Sathe
Sr Consultant
Capgemini Technology Services
------------------------------