1

I am trying to use the dynamic language run time / IronPython to simply run a .py script with command arguments and then get the std output. This is for executing the membase TAP protocol which isn't implemented in C# yet:

public class MembaseTap
{
    public void Tap()
    {
        var pyEngine = Python.CreateEngine(); 

        pyEngine.ExecuteFile(@"E:\Program Files\Membase\Server\bin\tap_example.py"); 


    }
}

I am able to use ExecuteFile to run the script, I believe, but I do not know how one would pass in arguments in this scenario if possible.

Essentially if I were running the command from the command line it would be:

python tap_example.py localhost:11210

I could simply run this from C# but that would require Python installed. That is one fall back option, but I'd prefer to use the DLR.

Any suggestions?

Sean Thoman
  • 7,429
  • 6
  • 56
  • 103

1 Answers1

2

You can pass in the arguments when you create the engine as shown by Jeff Hardy's answer in the Script arguments and Embedded IronPython post on StackOverflow. Your C# code would look like the following:

public class MembaseTap
{
    public void Tap()
    {
        var options = new Dictionary<string, object>();
        options["Arguments"] = new [] { "foo", "bar" };
        var pyEngine = Python.CreateEngine(options); 

        pyEngine.ExecuteFile(@"E:\Program Files\Membase\Server\bin\tap_example.py"); 
    }
}

To redirect the standard output you can use methods on the pyEngine.Runtime.IO object as shown in How can I redirect the stdout of ironpython in C#?.

Community
  • 1
  • 1
Matt Ward
  • 47,057
  • 5
  • 93
  • 94