2

I need to execute Python script from C# using IronPython and pass into it some command line parameters. Is there any way to do it?

My sample code:

        var e = Python.CreateEngine();
        var source = e.CreateScriptSourceFromFile(@"...");
        source.Execute();
1

1 Answer 1

4

Sure. When you create your engine, call the overload that takes in additional options and set the "Arguments" to your array of strings.

var options = new Dictionary<string, object>
{
    { "Arguments", new[] { "foo", "bar", "biz baz" } },
};
var engine = Python.CreateEngine(options);
var script = @"
import sys
print sys.argv # ['foo', 'bar', 'biz baz']
";
engine.Execute(script);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.