0

First off this is some is a dup (somewhat) of this: Running a python script on C# not working

My python script runs correctly from the command line as it is suppose to but when I run it from the console application it doesn't run correctly.

My python script is suppose to take all the .log files from a directory and pasrse them into a csv file. I then want to access the csv file from my C# console application. At the moment when I run the python script it creates the file then quits. Doesn't do anything past that.

Here is my code that I am using to attempt and run the python script. ( I am not and would not like to use IronPython ).

    public void runPythonScript()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("python");
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.FileName = @"C:\Python27\python.exe";
        startInfo.Arguments = @"C:\Users\bmahnke\Desktop\Python-Files\python3.py";  

        try
        {
            using (Process prog = Process.Start(startInfo))
            {

                prog.WaitForExit();
            }
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }
4
  • I'm sorry, I don't quite understand what you mean by 'it creates the file then quits'. What happens when you run the C# code, and how is it different from when you run the Python script manually? What is in the Python script?
    – Weeble
    Commented Feb 20, 2013 at 14:37
  • When I run the C# code a file is created called 'results.csv' in the directory that I specified its creaction in via the python script. The file is blank/nothing in it. When I run the python script from the command line I get the same file created/overwritten with the actual content of the file in there.
    – B-M
    Commented Feb 20, 2013 at 14:59
  • Another question: is the C# app built as a console app (/target:exe) or a GUI app (/target:winexe)?
    – Weeble
    Commented Feb 20, 2013 at 15:49
  • Are you able to post the Python script to pastebin or somewhere like that? If not, could you at least elaborate on what it is in it and what you're observing? What is the Python script's exit code? Does the same thing happen if you don't use CreateNoWindow? Can you determine how much of the Python script is executing before it stops?
    – Weeble
    Commented Feb 21, 2013 at 8:21

1 Answer 1

0

You've not given us much to go on. Here are a bunch of reasons you might get different behaviour when running a Python script from another program than when you run it from the console:

Different environment

A process normally inherits a copy of all its parent's environment variables when it is created, unless the parent specifically sets something differently. The environment variables that the Python process inherits from your console shell process may well be different from those inherited from the C# program which it inherited from whatever launched it. Important variables to check are PATH (the working directory), PYTHONPATH, PYTHONHOME, PYTHONUNBUFFERED and anything that your script specifically reads.

Redirected stdin, stdout, stderr

The standard file descriptors might behave differently depending on whether they're connected to a console, a file or piped to another process.

Presence of console

It's possible to start a process programmatically without a console attached. If your child process has no console at all and you haven't redirected its standard file descriptors, it will probably crash when it tries to read or write to them.

EDIT - I just tried this by creating a win32 C# program to invoke a console-based Python script and couldn't reproduce a crash - the console output from Python just disappears into the ether. I think I remember having had problems with this before, but I'm not sure what combination of factors might cause a problem. It might be worth investigating, but I couldn't be certain it's causing your trouble.

2
  • So how do I check the path that is being used when the console application is launched from VS2010?
    – B-M
    Commented Feb 20, 2013 at 16:13
  • You could inspect Environment.CurrentDirectory and Environment.GetEnvironmentVariables() in your C# program. You could try setting ProcessStartInfo.WorkingDirectory and ProcessStartInfo.EnvironmentVariables. If you can get your Python program to write output somewhere before it crashes you could inspect os.getcwd() and os.environ.
    – Weeble
    Commented Feb 20, 2013 at 16:56

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.