Discussion:
[Python.NET] Would you share your idea how to call python command from embedded Python.Net?
Seungweon Park
2013-04-01 18:00:02 UTC
Permalink
Hi,

I've been played with Python.Net for a week, but I can't find any sample to
use Python.Net in embedded way. I've searched many threads from the
previous emailing list, the result are not consistent, and looks like no
solution ??.

What I'm trying to do is that I want to get result (po) from C# code after
executing python command such as 'print 2+3' from python prompt.

When I executed it from nPython.exe, it prints out 5 as I expected.
However, when I run this code from embedded way. it returns 'null' always.
Would you give me some thoughts how I can get the execution result?

Basically, is this feature one of Python.Net project intended for C#(.Net)
to be able to call python commands/scripts?

Thank you,
Spark.

using NUnit.Framework;
using Python.Runtime;

namespace CommonTest
{
[TestFixture]
public class PythonTests
{
public PythonTests()
{

}
[Test]
public void CommonPythonTests()
{

PythonEngine.Initialize();

IntPtr gs = PythonEngine.AcquireLock();
PyObject po = PythonEngine.RunString("print 2+3");
PythonEngine.ReleaseLock(gs);

PythonEngine.Shutdown();
}
}
}
b***@public.gmane.org
2013-04-01 18:08:24 UTC
Permalink
Yes it is supposed to work.

But I'd point out that there is a big difference between the statements:

print 2+3

and

2+3

What exactly would you expect to get back as a return from the print statement?
Hi,
I've been played with Python.Net for a week, but I can't find any sample to use Python.Net in embedded way. I've searched many threads from the previous emailing list, the result are not consistent, and looks like no solution ??.
What I'm trying to do is that I want to get result (po) from C# code after executing python command such as 'print 2+3' from python prompt.
When I executed it from nPython.exe, it prints out 5 as I expected. However, when I run this code from embedded way. it returns 'null' always. Would you give me some thoughts how I can get the execution result?
Basically, is this feature one of Python.Net project intended for C#(.Net) to be able to call python commands/scripts?
Thank you,
Spark.
using NUnit.Framework;
using Python.Runtime;
namespace CommonTest
{
[TestFixture]
public class PythonTests
{
public PythonTests()
{
}
[Test]
public void CommonPythonTests()
{
PythonEngine.Initialize();
IntPtr gs = PythonEngine.AcquireLock();
PyObject po = PythonEngine.RunString("print 2+3");
PythonEngine.ReleaseLock(gs);
PythonEngine.Shutdown();
}
}
}
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
_________________________________________________
Python.NET mailing list - PythonDotNet-+ZN9ApsXKcEdnm+***@public.gmane.org
http://mail.python.org/mailman/listinfo/pythondotnet
Seungweon Park
2013-04-01 21:02:35 UTC
Permalink
Also wrote a simple console application using Python.Net,
But PythonEngine.RunString() returns 'null' so I got error.

namespace npythontest
{
class Program
{
static void Main(string[] args)
{
PythonEngine.Initialize();

IntPtr gs = PythonEngine.AcquireLock();
PyObject po = PythonEngine.RunString("2+3");

Trace.WriteLine("Python: " + po.ToString());

PythonEngine.ReleaseLock(gs);

PythonEngine.Shutdown();
}
}
}

Result is

Unhandled Exception: System.NullReferenceException: Object reference not
set toan instance of an object. at npythontest.Program.Main(String[] args)
in c:\temp\npythontest\npythontest
\Program.cs:line 20

I would expect above code will give some result. I think I'm stuck.
It's running in process with your .net code. So it's the same standard
output stream. A console's WriteLine() print statement should appear in
the same place. In VS, my recollection is that there is a specific panel
you can bring up in the debugger. Or, your project may be set to spawn an
external console. Depends on your configuration. I don't know NUnit all
that well. It's possible the NUnit's runtime redirects the output stream
somewhere else as well.
-brad
Well, would you tell me how to check the console when I execute the
PythonEngine.RunString("print 2+3")?
I'm running the line from VS2012 debugging mode, so I don't know how to
check the python console?
And have you checked the console to see if python is dutifully doing what
you are asking it to do, which is print "5" to the output stream? That
will not come back as a return value. You didn't ask it to give you an
object. You asked it to print 5 to the output console.
Hi Brad,
Well, I tried both 'print 2+3' and '2+3', but both returned 'null'.
I want to have both results of execution. That is, I want to have same
results whatever nPython.exe gives us.
Thanks,
Spark.
Post by b***@public.gmane.org
Yes it is supposed to work.
print 2+3
and
2+3
What exactly would you expect to get back as a return from the print statement?
Hi,
I've been played with Python.Net <http://python.net/> for a week, but
I can't find any sample to use Python.Net <http://python.net/> in
embedded way. I've searched many threads from the previous emailing list,
the result are not consistent, and looks like no solution ??.
What I'm trying to do is that I want to get result (po) from C# code
after executing python command such as 'print 2+3' from python prompt.
When I executed it from nPython.exe, it prints out 5 as I expected.
However, when I run this code from embedded way. it returns 'null' always.
Would you give me some thoughts how I can get the execution result?
Basically, is this feature one of Python.Net <http://python.net/>project intended for C#(.Net) to be able to call python commands/scripts?
Thank you,
Spark.
using NUnit.Framework;
using Python.Runtime;
namespace CommonTest
{
[TestFixture]
public class PythonTests
{
public PythonTests()
{
}
[Test]
public void CommonPythonTests()
{
PythonEngine.Initialize();
IntPtr gs = PythonEngine.AcquireLock();
PyObject po = PythonEngine.RunString("print 2+3");
PythonEngine.ReleaseLock(gs);
PythonEngine.Shutdown();
}
}
}
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...