How to use Python in a .NET-Core application?

How to use Python in a .NET-Core application? I need this for Hackathon's purposes, so the solution does not have to be "elegant." I read that it is not easy to run Python scripts directly, because there is only IronPython library for standard ASP.NET, but not for .NET-Core. So what is the easiest way to use Python scripts? (Because hackathon it is normal to use even a PHP server or selenium, etc. Only to execute a script)

+6
source share
1 answer

try it

public class RunCmd { public string Run(string cmd, string args) { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = "python"; start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args); start.UseShellExecute = false;// Do not use OS shell start.CreateNoWindow = true; // We don't need new window start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions) using (Process process = Process.Start(start)) { using (StreamReader reader = process.StandardOutput) { string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test") return result; } } } } 

Then

  var res = new RunCmd().Run("your_python_file.py","params"); Console.WriteLine(res); 
+1
source

Source: https://habr.com/ru/post/1013040/


All Articles