[Python.NET] Would you share your idea how to call python command from embedded Python.Net?

Seungweon Park swpark71 at gmail.com
Mon Apr 1 23:02:35 CEST 2013


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.

On Mon, Apr 1, 2013 at 11:32 AM, brad at fie.us <brad at fie.us> wrote:

> 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
>
> On Apr 1, 2013, at 2:29 PM, Seungweon Park <swpark71 at gmail.com> wrote:
>
> 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?
>
> On Mon, Apr 1, 2013 at 11:23 AM, brad at fie.us <brad at fie.us> wrote:
>
>> 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.
>>
>> On Apr 1, 2013, at 2:21 PM, Seungweon Park <swpark71 at gmail.com> wrote:
>>
>> 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.
>>
>> On Mon, Apr 1, 2013 at 11:08 AM, brad at fie.us <brad at fie.us> wrote:
>>
>>> 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?
>>>
>>> On Apr 1, 2013, at 2:00 PM, Seungweon Park <swpark71 at gmail.com> wrote:
>>>
>>> > 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();
>>> >         }
>>> >     }
>>> > }
>>> >
>>> > _________________________________________________
>>> > Python.NET <http://python.net/> mailing list - PythonDotNet at python.org
>>> > http://mail.python.org/mailman/listinfo/pythondotnet
>>>
>>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20130401/646b3396/attachment.html>


More information about the PythonDotNet mailing list