Since all you are telling it to do is run a script and you are looking to capture it's output, I'd just use System.Diagnostics.Process to run "python.exe" with the args "c:\\temp\\a.py".  Then I'd use the returned Process object's Output stream object to read the lines from it's standard output and standard error.  No PythonNet required.  The other way to do it if you insist on using PythonNet to embed python rather than just spawn it as a subprocess, you can redirect use Console to redirect the streams for the running process yourself.  That should probably work as well.


On Apr 5, 2013, at 12:48 PM, Seungweon Park <swpark71@gmail.com> wrote:

WOW. Now I got it. Thank you much for the kind explanation. Now it works as charm in Console Application in Windows system.
In the meantime, if it is not console window application, how I can see/capture the text output? (I know it will work but want to capture the output)

Any idea?


On Thu, Apr 4, 2013 at 6:38 PM, brad@fie.us <brad@fie.us> wrote:
It's an escape character issue.

Since you are pasting source code in your source code, you need to double escape.  Probably as follows:

PythonEngine.RunSimpleString("execfile('c:\\\\temp\\\\a.py')");

the c# parser turns that into:  execfile('c:\\temp\\a.py')

And then python runs those escape characters and gets:  c:\temp\a.py

If you don't double escape, C# sends: execfile('c:\temp\a.py')

And therefore the escape characters \t and \a are executed, resulting in that weird value you got.


On Apr 4, 2013, at 5:45 PM, Seungweon Park <swpark71@gmail.com> wrote:

I retested with same code as you have and added online to execute a.py. It looks it running but not the last line

        po = PythonEngine.RunString("execfile('c:\\temp\\a.py')");


<image.png>



So I changed to

     PythonEngine.RunSimpleString("execfile('c:\\temp\\a.py')");

then, I got 

<image.png>

Hello World!
<module 'ntpath' from 'C:\Python27\Lib\ntpath.pyc'>
my\path
other\path
5
Traceback (most recent call last):
  File "<string>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'c:\temp\x07.py'

I don't know why it converted to 'c:\temp\x07.py' from 'c:\temp\a.py', and got the error. Any idea?




On Thu, Apr 4, 2013 at 2:25 PM, Seungweon Park <swpark71@gmail.com> wrote:
As you see the screenshot, I ran same application like you, but it doesn't have any output which a.py's supposed to print 'aa' in the screen after "Hello World!".


<image.png>


On Thu, Apr 4, 2013 at 1:51 PM, brad@fie.us <brad@fie.us> wrote:
What happens when you run a compiled application like mine, that has both c# and python console output, directly from within the windows command line cmd.exe?  Do you see the WriteLine() results from C# but not PythonNet?

That will tell you if it's an execution environment issue or an actual programming issue.  Which is still in question I think.

-brad

On Apr 4, 2013, at 4:40 PM, Seungweon Park <swpark71@gmail.com> wrote:

Image file is not attached. Here it goes.


On Thu, Apr 4, 2013 at 1:37 PM, Seungweon Park <swpark71@gmail.com> wrote:
Hi,

Actually, my questions are 

            1. How to turn on console window(live) from Windows System while debugging Embedded PythonNet app?
            2. How to capture python script execution output from C# code?

I've been talking to Brad@fie.us about my issue which I can't get the output result of any python command using PythonEngine.RunString() or PythonEngine.RunSimpleString(). 
I've been tried to debug simple code with Python.Runtime project, but can't find how to turn on this console window, nor capture the python script execution output.

What I'm trying to do is that I don't want to modify any python script when running a python script from C# code, so I have below code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Python.Runtime;
using System.Diagnostics;

namespace npythontest
{
public class Program
{
static void Main(string[] args)
{
PyObject po;

PythonEngine.Initialize();

IntPtr pythonLock = PythonEngine.AcquireLock();
po = PythonEngine.RunString("execfile('c:\\cvs\\brocade.py')");
PythonEngine.ReleaseLock(pythonLock);

PythonEngine.Shutdown();
}
}
}

and a.py prints out many string output. I want to capture this output  by RunString()/RunSimpleString() method, but it doesn't return any result except 'null'. It seems it isn't for getting output. 

Then, while talking to Brad, his screenshot from his Mac OS with Mono displays the execution result output on an window like below, so wonder if windows environment can have same console window.
Previously, Oleksii and Sharon mentioned in the thread http://mail.python.org/pipermail/pythondotnet/2011-March/001103.html, but don't know it's already implemented or not. However, I can't find how to have same environment from my window system like Brad's Mac has.

So would you help me how to capture the python script output or have console window for debugging when I'm using embedded PythonNet? 

Thank you,
Spark.

<Screen Shot 2013-04-01 at 6.22.50 PM.png>


<mono_screenshot_pythonnet.png>