Kratos
Kratos

Reputation: 205

how to run a python code using cmd.exe from a C# application?

I have an application where i am trying to run a python from a c# application. i have tried creating the python runtime environment and run the code, but as my python code is importing some modules from another python file it throws an exception (import exception). i have tried the following code:

var ipy = Python.CreateRuntime();
                dynamic test = ipy.UseFile(@"file path");
                test.Simple();
                Console.Read();

I hvae another idea of running it through cmd prompt, but i don't know how do it. i want open to cmd.exe and execute the python file and i want it such that the user enters the filename in c# aplication and on clicking the run button the code is executed through cmd.exe and the output is again shown in c# application. Any other suggestions are also welcome.

Upvotes: 0

Views: 4298

Answers (3)

Kratos
Kratos

Reputation: 205

i have tried the following code and it seems to solve my problem:

Process p = new Process();
            string cmd = @"python filepath & exit";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.RedirectStandardInput = true;
            p.Start();
            StreamWriter myStreamWriter = p.StandardInput;
            myStreamWriter.WriteLine(cmd.ToString());
            myStreamWriter.Close();
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            Console.ReadLine();

Upvotes: 0

Rami Yampolsky
Rami Yampolsky

Reputation: 475

That would do the job: the following example runs cmd which runs TCL script (that wat I have installed on my computer) you only need to replace the command to run Python and add your script file. Pay attention to the " & exit" comming after your script file name - this makes the cmd exit after your script exits.

string fileName = "C:\\Tcl\\example\\hello.tcl";
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo("cmd", "/K tclsh " + fileName + " & exit")
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        Console.WriteLine(output);

        Console.ReadLine();

[Update]

After Python installation and testing, that would be the code to run python script with cmd:

 string fileName = @"C:\Python27\example\hello_world.py";

        Process p = new Process();
        p.StartInfo = new ProcessStartInfo("cmd", "/K " + fileName + " & exit")
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        Console.WriteLine(output);

        Console.ReadLine();

Also you can do the same without the CMD process:

string fileName = @"C:\Python27\example\hello_world.py";

        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"C:\Python27\python.exe",  fileName )
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };
        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        Console.WriteLine(output);

        Console.ReadLine();

Upvotes: 1

Konrad Gadzina
Konrad Gadzina

Reputation: 3404

I cannot test it personally at the moment, but I found some people using Python.CreateEngine() in their code, example:

Microsoft.Scripting.Hosting.ScriptEngine engine = 
    IronPython.Hosting.Python.CreateEngine();

This line was taken from this SO question.

You can also check this article with example class using python code. It also uses Python.CreateEngine().

Upvotes: 0

Related Questions