0

This sort of question has been asked before in varying degrees, but I feel it has not been answered in a concise way and so I ask it again.

I want to run a script in Python. Let's say it's this:

def func1():
    list = [1,2,3,4,5]
    return list

Which gets the empty result.when putting print statement its return value to c#.but I want a result without printing in python. My actual code result is very big.so it takes time.

Okay, so how do I run this in C#?

This is what I have now:

   var process = new Process () {
  StartInfo = new ProcessStartInfo {
  FileName = "python",
  Arguments = "-u /home/varun/Desktop/data.py",
  RedirectStandardOutput = true,
  RedirectStandardError = true,
  UseShellExecute = false,
  CreateNoWindow = true,
  }
};
process.Start ();
string output = process.StandardOutput.ReadToEnd ();
string error = process.StandardError.ReadToEnd ();
process.WaitForExit ();
if (string.IsNullOrEmpty (error)) { return output; } else {
  Console.WriteLine (error);
  return error;
}
leo Martin
  • 180
  • 1
  • 3
  • 15
  • 1
    You'll probably need IronPython or something similar to it. – Sweeper Sep 19 '19 at 12:20
  • can you give an example ? – leo Martin Sep 19 '19 at 12:21
  • Option 3 mentioned in [this answer](https://stackoverflow.com/a/7060760/5133585) is probably what you want. This question is probably a duplicate of that. – Sweeper Sep 19 '19 at 12:24
  • @Sweeper this question is not about iron python. – leo Martin Sep 19 '19 at 12:29
  • @Sweeper Also, how do I return the result from python.?.that part is missing this answer[https://stackoverflow.com/a/7060760/5133585] – leo Martin Sep 19 '19 at 12:31
  • Actually, you should use the [answer by LiRoN](https://stackoverflow.com/a/53609971/5133585) because Option 3 doesn't seem to work anymore apparently. By "how do I return the result from python.?", do you mean how do you get the return value? After you got the function, just call the function in C# like a delegate and get the return value. This is all shown in the answer. – Sweeper Sep 19 '19 at 12:36
  • @Sweeper dynamic testFunction = scope.GetVariable("test_func"); var result = testFunction(var1,var2); this part is not compling in my code im using Microsoft.CSharp 4.5 – leo Martin Sep 19 '19 at 12:51
  • What's the error message? "not compiling" is too vague. – Sweeper Sep 19 '19 at 12:51
  • Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' – leo Martin Sep 19 '19 at 12:54

0 Answers0