3

Question update

I'm trying to use a C# script in Unity to call a python program. After following these two links:

How do I run a Python script from C#?

Process.Start() not starting the .exe file (works when run manually)

I could not get my C# script to call the python program. I know this isn't a problem with my python script because I can run the program in terminal without a problem.

public class JsonStream : MonoBehaviour
{
    private string GetStream()
    {
        ProcessStartInfo start = new ProcessStartInfo();

        //none of the paths work, the uncommented variables return true when using File.Exists
        //while the commented variables return false
        start.FileName = "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3";
        //start.FileName = "\"/Library/Frameworks/Python.framework/Versions/3.6/bin/python3\"";

        start.Arguments = Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch/stream.py";
        //start.Arguments = "\"" + Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch/stream.py" + "\"";

        start.WorkingDirectory = Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch";
        //start.WorkingDirectory = "\"" + Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch" + "\"";

        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.ErrorDialog = true;
        start.RedirectStandardError = true;

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                return result;
            }
        }
    }

    public void DoEverythingStream()
    {
        UnityEngine.Debug.Log("Doing Stream");
        string json_text = GetStream();
        string[] games = json_text.Trim().Split('\n');
        foreach (string game in games)
        {
            UnityEngine.Debug.Log(game);
        }
    }
}

Running the program raises no errors but also doesn't output anything. Does anyone have any ideas what may be wrong with my program? Thanks!

brian
  • 31
  • 3
  • What are the **exact** values of `start.FileName` and `start.Arguments` and `start.WorkingDirectory`? _Please debug and check - don't guess._ – mjwills Jul 05 '18 at 07:07
  • @mjwills Logging the values in console returns "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3", "/Users/brian/Documents/Unity Projects/Project 1/Assets/googlesheetspyprojectbatch/stream.py", and "/Users/brian/Documents/Unity Projects/Project 1/Assets/googlesheetspyprojectbatch/stream.py" _without_ the quotes, respectively. The commented out version returns the same, but this time _with_ the quotes. – brian Jul 05 '18 at 07:19
  • Currently this is running on my 10.13.1 Macbook Pro. I hope to get this working on windows in the future and will change the directories to correspond to windows when I do. – brian Jul 05 '18 at 09:22

1 Answers1

0

Old question, but I was struggling to figure out why C# (Unity) wasn't running my Python file even though a manual command prompt could.

By putting my python script's path in triple double quotes ("""), C# was able to run the script.

Example:

string fileName = @"""PATH\TO\YOUR\SCRIPT.PY""";
string pythonPath = @"PATH\TO\PYTHON.EXE";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo(pythonPath, fileName)
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
p.Start();

Sludge
  • 6,072
  • 5
  • 31
  • 43
  • Could be that there are spaces in the `PATH\TO\YOUR\SCRIPT.PY`? This would explain why the command would fail without wrapping in `"` – Pieterjan Apr 24 '22 at 18:33