2

I want to run python code from C# through command Prompt.The Code is attached below

    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.WorkingDirectory = @"d:";
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;

    p.Start();
    p.StandardInput.WriteLine(@"cd D:\python-source\mypgms");
    p.StandardInput.WriteLine(@"main.py -i example-8.xml -o output-8.xml");

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

    Console.WriteLine("Output:");
    Console.WriteLine(output);

Output :

D:\python-source\mypgms>main.py -i example-8.xml -o output-8.xml

D:\python-source\mypgms>

But nothing happened.Actually main.py is my main program and it takes 2 arguments. one is input xml file and another one is converted output xml file.

But i dont know how to run this python script from C# through command prompt. Please Guide me to get out of this issue...

Thanks & Regards, P.SARAVANAN

2
  • 1
    what were you expecting to happen? And why involve cmd.exe? Why not run python.exe? Commented Apr 21, 2012 at 7:04
  • 2
    the main issue here is that cmd.exe does not interpret python code. you need python.exe to do that. Commented Apr 21, 2012 at 8:19

3 Answers 3

5

I think you are mistaken in executing cmd.exe. I'd say you should be executing python.exe, or perhaps executing main.py with UseShellExecute set to true.

At the moment, your code blocks at p.WaitForExit() because cmd.exe is waiting for your input. You would need to type exit to make cmd.exe terminate. You could add this to your code:

p.StandardInput.WriteLine(@"exit");

But I would just cut out cmd.exe altogether and call python.exe directly. So far as I can see, cmd.exe is just adding extra complexity for absolutely no benefit.

I think you need something along these lines:

var p = new Process();
p.StartInfo.FileName = @"Python.exe";
p.StartInfo.Arguments = "main.py input.xml output.xml";
p.StartInfo.WorkingDirectory = @"D:\python-source \mypgms";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();

Also the Python script appears to output to a file rather than to stdout. So when you do p.StandardOutput.ReadToEnd() there will be nothing there.

Sign up to request clarification or add additional context in comments.

1 Comment

@Thanks David Heffernan: It works fine...But the Command prompt window immediately closed.how to prevent the window automatically closed?...
2

Why not host IronPython in your app and then execute the script?

http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx

http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application

1 Comment

That's another option, but it doesn't really address the problem here. It's no problem at all to invoke CPython from C#. It just needs to be done right. Surely it would be quicker to make a couple of simple changes to the code rather than switch to hosting IronPython?
0

or use py2exe to pragmatically convert your python script to exe program.

detail steps...

  • download and install py2exe.
  • put your main.py input.xml and output.xml in c:\temp\
  • create setup.py and put it in folder above too

setup.py should contain...

from distutils.core import setup
import py2exe

setup(console=['main.py'])

your c# code then can be...

var proc = new Process();
proc.StartInfo.FileName = @"Python.exe";
proc.StartInfo.Arguments = @"setup.py py2exe";
proc.StartInfo.WorkingDirectory = @"C:\temp\";
proc.Start();
proc.WaitForExit();

proc.StartInfo.FileName = @"C:\temp\dist\main.exe";
proc.StartInfo.Arguments = "input.xml output.xml";
proc.Start();
proc.WaitForExit();

6 Comments

Then the code in the Q would give the same behaviour, but there would be an extra complication in the build process.
it won't give the same behavior. he can choose to convert main.py to main.exe by hand or pragmatically. then invoke main.exe directly with process. of course, this approach will require python anyway, but it's just another option.
It will give the same behaviour if he keeps on invoking through cmd.exe. The whole point of Python exe wrappers is that they behave the same as running the script through the interpretor. Your answer does not address the fundamental problem.
This is astonishinghly poor advice. You have shown how to invoke python.exe from C# so you just do that and pass the .py file as argument. py2exe is an utterly crazy idea here. The point is to avoid cmd.exe which you do. And incidentally is the main point of my answer.
@RayCheng : It throwing error like import error:no module named py2exe...what is the problem here?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.