I'm trying to use Unity C# (don't worry, easy to port to normal C# but I don't currently have a program that lets me do so) to run a python application using the following code, which basically just starts a python program and reads and writes some input and output:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
public class PythonSetup : MonoBehaviour {
// Use this for initialization
void Start () {
SetupPython ();
}
void SetupPython() {
string fileName = @"C:\sample_script.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(pythonExe, "YOUR PYTHON3 PATH")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
UnityEngine.Debug.Log (p.StandardOutput.ReadToEnd ());
p.StandardInput.WriteLine ("\n hi \n");
UnityEngine.Debug.Log(p.StandardOutput.ReadToEnd());
p.WaitForExit();
}
}
The python application, located at C:/sample_script.py, is:
print("Input here:")
i = input()
print(i)
The C# program gives me the error:
InvalidOperationException: Standard input has not been redirected System.Diagnostics.Process.get_StandardInput () (wrapper remoting-invoke-with-check) System.Diagnostics.Process:get_StandardInput ()
Thanks for the help ahead of time!
To put into normal C# project, just replace UnityEngine.Debug.Log with Console.WriteLine and replace Start() with Main().