Discussion:
[Python.NET] How to run a command by Embedded Python?
Seungweon Park
2013-03-28 21:35:29 UTC
Permalink
Hi,

I wrote a code PythonWrapper and SimplePythonTest which uses PythonWrapper.


using SimplePython;

namespace SimplePythonTest
{
/// <summary>
/// Python Wrapper Tests
/// </summary>
[TestFixture]
public class PythonTests
{
PythonWrapper py;
/// <summary>
/// PythonTests Constructor
/// </summary>
public PythonTests()
{
py = new PythonWrapper();

py.Init();

PyObject po;
po = py.LoadModule("os");
po = py.LoadModule("sys");
po = py.LoadModule("psutil");
}

/// <summary>
/// Python Simple Test
/// </summary>
[Test]
public void CommonPythonTests()
{
PyObject po = py.Execute("os.getcwd()");
Trace.WriteLine(po.Repr());

py.Close();
}
}
}


namespace SimplePython
{
/// <summary>
/// Python Wrapper via XMLRPC/Python 2.7.3/.Net for Python.
/// </summary>
public class PythonWrapper
{
/// <summary>
/// Inter Lock
/// </summary>
private IntPtr gs;

/// <summary>
/// Constructor
/// </summary>
public PythonWrapper()
{
}

/// <summary>
/// Initialize PythonEngine
/// </summary>
public void Init()
{
PythonEngine.Initialize();
gs = PythonEngine.AcquireLock();

}

/// <summary>
/// Load Module
/// </summary>
/// <param name="strModule">Module Name</param>
/// <returns></returns>
public PyObject LoadModule(string strModule)
{
return PythonEngine.ImportModule(strModule);
}

/// <summary>
/// Execute Python Command
/// </summary>
/// <param name="strCommand">Python Command</param>
/// <returns></returns>
public PyObject Execute(string strCommand)
{
PyObject po = PythonEngine.RunString(strCommand);

return po;
}

public void Close()
{
PythonEngine.ReleaseLock(gs);
PythonEngine.Shutdown();
}
}
}

When testing (CommonPythonTests), it initializes and loads modules, but
PythonEngine.RunString() doesn't seem to work properly. Is any something
wrong in my code which gets os.getcwd()? Furthermore, I can't find the
difference between PythonEngine.RunString() and
PythonEngine.RunSimpleString().

Thank you,
Spark.
Seungweon Park
2013-03-28 22:22:56 UTC
Permalink
BTW, PyObject po = PythonEngine.RunString(strCommand); returns 'null' when
running "os.getcwd()", and PythonEngine.RunSimpleString(strCommand) returns
'-1'.

Currently, I'm working on Async ways to execute a python command (some
commands take long to finish a task such as switch configuration, once
executing a command, and returns, then gets a result later).
Would you give me some suggestions how/what would be best idea for Async
wrapper using Python.RunTime?

Thanks,
Spark.
Post by Seungweon Park
Hi,
I wrote a code PythonWrapper and SimplePythonTest which uses PythonWrapper.
using SimplePython;
namespace SimplePythonTest
{
/// <summary>
/// Python Wrapper Tests
/// </summary>
[TestFixture]
public class PythonTests
{
PythonWrapper py;
/// <summary>
/// PythonTests Constructor
/// </summary>
public PythonTests()
{
py = new PythonWrapper();
py.Init();
PyObject po;
po = py.LoadModule("os");
po = py.LoadModule("sys");
po = py.LoadModule("psutil");
}
/// <summary>
/// Python Simple Test
/// </summary>
[Test]
public void CommonPythonTests()
{
PyObject po = py.Execute("os.getcwd()");
Trace.WriteLine(po.Repr());
py.Close();
}
}
}
namespace SimplePython
{
/// <summary>
/// Python Wrapper via XMLRPC/Python 2.7.3/.Net for Python.
/// </summary>
public class PythonWrapper
{
/// <summary>
/// Inter Lock
/// </summary>
private IntPtr gs;
/// <summary>
/// Constructor
/// </summary>
public PythonWrapper()
{
}
/// <summary>
/// Initialize PythonEngine
/// </summary>
public void Init()
{
PythonEngine.Initialize();
gs = PythonEngine.AcquireLock();
}
/// <summary>
/// Load Module
/// </summary>
/// <param name="strModule">Module Name</param>
/// <returns></returns>
public PyObject LoadModule(string strModule)
{
return PythonEngine.ImportModule(strModule);
}
/// <summary>
/// Execute Python Command
/// </summary>
/// <param name="strCommand">Python Command</param>
/// <returns></returns>
public PyObject Execute(string strCommand)
{
PyObject po = PythonEngine.RunString(strCommand);
return po;
}
public void Close()
{
PythonEngine.ReleaseLock(gs);
PythonEngine.Shutdown();
}
}
}
When testing (CommonPythonTests), it initializes and loads modules, but
PythonEngine.RunString() doesn't seem to work properly. Is any something
wrong in my code which gets os.getcwd()? Furthermore, I can't find the
difference between PythonEngine.RunString() and
PythonEngine.RunSimpleString().
Thank you,
Spark.
b***@public.gmane.org
2013-03-28 22:38:24 UTC
Permalink
Doesn't it just depend on where you want the async to be? On the python side or on the .net side? You could do it all in the python side and just stick to python async methodologies. Or you could do it in .net in which case you need to use the multi-threaded aspects of pythonnet.

-brad
BTW, PyObject po = PythonEngine.RunString(strCommand); returns 'null' when running "os.getcwd()", and PythonEngine.RunSimpleString(strCommand) returns '-1'.
Currently, I'm working on Async ways to execute a python command (some commands take long to finish a task such as switch configuration, once executing a command, and returns, then gets a result later).
Would you give me some suggestions how/what would be best idea for Async wrapper using Python.RunTime?
Thanks,
Spark.
Hi,
I wrote a code PythonWrapper and SimplePythonTest which uses PythonWrapper.
using SimplePython;
namespace SimplePythonTest
{
/// <summary>
/// Python Wrapper Tests
/// </summary>
[TestFixture]
public class PythonTests
{
PythonWrapper py;
/// <summary>
/// PythonTests Constructor
/// </summary>
public PythonTests()
{
py = new PythonWrapper();
py.Init();
PyObject po;
po = py.LoadModule("os");
po = py.LoadModule("sys");
po = py.LoadModule("psutil");
}
/// <summary>
/// Python Simple Test
/// </summary>
[Test]
public void CommonPythonTests()
{
PyObject po = py.Execute("os.getcwd()");
Trace.WriteLine(po.Repr());
py.Close();
}
}
}
namespace SimplePython
{
/// <summary>
/// Python Wrapper via XMLRPC/Python 2.7.3/.Net for Python.
/// </summary>
public class PythonWrapper
{
/// <summary>
/// Inter Lock
/// </summary>
private IntPtr gs;
/// <summary>
/// Constructor
/// </summary>
public PythonWrapper()
{
}
/// <summary>
/// Initialize PythonEngine
/// </summary>
public void Init()
{
PythonEngine.Initialize();
gs = PythonEngine.AcquireLock();
}
/// <summary>
/// Load Module
/// </summary>
/// <param name="strModule">Module Name</param>
/// <returns></returns>
public PyObject LoadModule(string strModule)
{
return PythonEngine.ImportModule(strModule);
}
/// <summary>
/// Execute Python Command
/// </summary>
/// <param name="strCommand">Python Command</param>
/// <returns></returns>
public PyObject Execute(string strCommand)
{
PyObject po = PythonEngine.RunString(strCommand);
return po;
}
public void Close()
{
PythonEngine.ReleaseLock(gs);
PythonEngine.Shutdown();
}
}
}
When testing (CommonPythonTests), it initializes and loads modules, but PythonEngine.RunString() doesn't seem to work properly. Is any something wrong in my code which gets os.getcwd()? Furthermore, I can't find the difference between PythonEngine.RunString() and PythonEngine.RunSimpleString().
Thank you,
Spark.
_________________________________________________
http://mail.python.org/mailman/listinfo/pythondotnet
Loading...