4

How do I pass command line parameters from my C# application to IronPython 2.x? Google is only returning results about how to do it with Iron Python 1.x.

static void Main(string[] args)
{
    ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();
    // Pass in script file to execute but how to pass in other arguments in args?
    ScriptScope scope = scriptRuntime.ExecuteFile(args[0]);
}
2
  • 1
    And 1.x solution doesn't work in 2.x? Commented Jun 1, 2012 at 15:14
  • 1.x uses scriptEngine.Sys.argv which was removed in 2.x sadly. Commented Jun 1, 2012 at 15:44

1 Answer 1

5

You can either set the sys.argv via the following C# code:

static void Main(string[] args)
{
    var scriptRuntime = Python.CreateRuntime();
    var argv = new List();
    args.ToList().ForEach(a => argv.Add(a));
    scriptRuntime.GetSysModule().SetVariable("argv", argv);
    scriptRuntime.ExecuteFile(args[0]);
}

having the following python script

import sys
for arg in sys.argv:
    print arg

and calling the exe like

Test.exe SomeScript.py foo bar

gives you the output

SomeScript.py
foo
bar

Another option would be passing the prepared options to Python.CreateRuntime as explained in this answer

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.