'How to run python script from C# code by using command

I have a script file(run_edr.py) in my local machine and when I run it by using "cmd" and the following command then the script works perfectly. The script takes fewer parameters, the first parameter is an input document folder path and the second parameter is the output folder path to store the output documents.

my python command,

python run_edr.py -input_path "C:\Users\aslamm5165\Downloads\EDRCODE_ArgParser\files\EDR" -output_path "C:\Users\aslamm5165\Downloads\test" -site_name "a" -site_address "b" -site_city "c" -site_county "d" -site_state "e" -site_type "1"

I have tried like below, but not working, where Did I go wrong?

ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
        ScriptRuntime runtime = new ScriptRuntime(setup);
        ScriptEngine engine = Python.GetEngine(runtime);
        ScriptSource source = engine.CreateScriptSourceFromFile(@"C:\Users\aslamm5165\Downloads\EDRCODE_ArgParser\run_edr.py");
        ScriptScope scope = engine.CreateScope();
        List<String> argv = new List<String>();
        //Do some stuff and fill argv
        argv.Add("python"+@" C:\Users\aslamm5165\Downloads\EDRCODE_ArgParser\run_edr.py -input_path" + @"C:\Users\aslamm5165\Downloads\EDRCODE_ArgParser\files\EDR");
        argv.Add("-output_path"+ @"C:\Users\aslamm5165\Downloads\test");
        argv.Add("-site_name 'a' -site_address 'b' -site_city 'c' -site_county 'd' -site_state 'e' -site_type '1'");
        engine.GetSysModule().SetVariable("argv", argv);
        source.Execute(scope);

I have tried with the system process as well as shown below, no error in the code, but the script is not getting executed. So I don't know what is the correct way of doing this but I want to start my script from my .Net Core application.

ProcessStartInfo start = new ProcessStartInfo();            
        start.FileName = @"cmd.exe";            
        start.Arguments = string.Format("python run_edr.py -input_path {0} -output_path {1}  -site_name 'a' -site_address 'b' -site_city 'c' -site_county 'd' -site_state 'e' -site_type '1'", @"C:\Users\aslamm5165\Downloads\EDRCODE_ArgParser\files\EDR", @"C:\Users\aslamm5165\Downloads\test");
        start.UseShellExecute = true;
        start.RedirectStandardOutput = false;
        start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        Process.Start(start);


Solution 1:[1]

I solved a similar running Python scripts in .NET Core 3.1 problem by changing the executable file from cmd.exe or /bin/bash in Linux to a batch script (Windows) or shell script (Linux) file. Here's my approach:

1, for Windows OS, create a run.bat file which include the python.exe and the %* to pass all arguments to it:

C:\YOUR_PYTHON_PATH\python.exe %*

2, for LInux OS, create a run.sh file to execute python with arguments:

#!/bin/bash
/usr/bin/python3 "$@"

3, use Process and ProcessStartInfo (your second approach):

string fileName = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    fileName = "path_to_bat/run.bat"
}
else
{
    fileName = "path_to_bat/run.sh"
}

ProcessStartInfo start = new ProcessStartInfo
{
    FileName = fileName,
    Arguments = string.Format("\"{0}\" \"{1}\"", script, args),
    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};
using Process process = Process.Start(start);

the .NET code is same to Windows except the FileName should be the shell script's name with path.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lushang