1

I'm attempting to understand how to execute a Python script file from a C# program. I'm using Python 3.10 and Visual Studio 2022 for my C# program.

Since this appears to be a very popular question here, I looked at quite a few other posts & responses regarding this. However, I'm still not quite understand what the proper way to do this, as I'm struggling to call even a basic script with a single supplied argument.

I understand that there is a method for doing this using the IronPython library, however I must find a way do this without installing any additional libraries. The most common other way I found across these posts is to use the Process in-built library.

I have a test Python script named test.py, which I placed in the root folder of my C# program, which simply prints out the single argument supplied to the script:

import sys

if (__name__ == '__main__'):
    print(sys.argv[1])

The closest I've been able to get is the following C# code from this StackOverflow post:

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Program Files\\Python310\\python.exe";
start.Arguments = string.Format("{0} {1}", "test.py", "a");
start.UseShellExecute = false;
start.RedirectStandardOutput = true;

using (Process process = Process.Start(start))
{
    using (StreamReader reader = process.StandardOutput)
    {
        string result = reader.ReadToEnd();
        OutputConsole.AppendText(result + Environment.NewLine);
    }
}

Execution of the code above results in the result string local variable always being null. Another thing I also noticed is that the script doesn't give me any sort of error message or prompt if I move the test.py script file somewhere else.

In the StackOverflow post above, one of the answers mentions the fact that if the UseShellExecute field of the start local variable is set to false, then I must always supply the full path to my Python interpreter EXE file as the first argument of the Arguments field of the start variable.

What am I doing wrong here? Is there another way of doing this without using additional libraries?

Thanks for reading my post, any guidance is appreciated.

Runsva
  • 365
  • 1
  • 7
  • _"however I must find a way do this without installing any additional libraries."_ - says who? – Fildor May 02 '23 at 06:51
  • What happens when you run the command line your building directly in your shell? – Good Night Nerd Pride May 02 '23 at 07:30
  • Thank you for your reply; If I try to run the `test.py` Python script by itself, I get an index out of bounds error, as if I run it standalone I'm assuming there's nothing in the `argv` field. – Runsva May 02 '23 at 07:46
  • That error is probably the reason python doesn't write anything to stdout. It's writing to stderr instead. Try reading the error output as well: https://stackoverflow.com/questions/5005874/how-to-get-the-error-message-of-a-process – Good Night Nerd Pride May 02 '23 at 07:57
  • 1
    Just to make sure I was understood correctly: Try running `"C:\Program Files\Python310\python.exe" test.py a` in the command line or `& "C:\Program Files\Python310\python.exe" test.py a` in PowerShell. – Good Night Nerd Pride May 02 '23 at 08:00
  • Thank you for your reply; If I run the script with the command line you showed above, I get the desired output (just a single `a`). But if I try to run my C# program, I get nothing as an output. – Runsva May 03 '23 at 01:30

1 Answers1

0

After a bit of meddling, I found out that the mistake here was quite trivial.

Alongside the full path of the Python interpreter EXE, the path to the Python script also has to be provided in FULL, not relative to the C# project root folder.

Runsva
  • 365
  • 1
  • 7