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;
}
9
  • 1
    You'll probably need IronPython or something similar to it. Commented Sep 19, 2019 at 12:20
  • can you give an example ? Commented Sep 19, 2019 at 12:21
  • Option 3 mentioned in this answer is probably what you want. This question is probably a duplicate of that. Commented Sep 19, 2019 at 12:24
  • @Sweeper this question is not about iron python. Commented Sep 19, 2019 at 12:29
  • @Sweeper Also, how do I return the result from python.?.that part is missing this answer[stackoverflow.com/a/7060760/5133585] Commented Sep 19, 2019 at 12:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.