4

I have an embedded scripting engine in my C# application that uses IronPython 2. I create the Python runtime and add a few classes to the global namespace so that the script can import them as modules.

However, one (pretty basic) thing I can't figure out is how to send script arguments. I realize that I could just create a variable with a list of arguments, but there must be a proper way to do it. Also, doing it in the proper 'Python' way allows me to compile the scripts and use an automated document builder called Sphinx. So the ultimate goal is to be able to use:

import sys
sys.argv

In one of my scripts and have it get the arguments the user specified (through the C# app).

Right now, I call the script by using:

// set up iron python runtime engine
_engine = Python.CreateEngine();
_runtime = _engine.Runtime;
_scope = _engine.CreateScope();

// run script
_script = _engine.CreateScriptSourceFromFile(_path);
_script.Execute(_scope);

And I've tried searching for an API to add script arguments with no luck. I've also tried appending them to the script path (_path in example) with no luck. I tried with CreateScriptSourceFrom File and CreateScriptSourceFromSting (which was a long shot anyway...).

Is what I'm trying to do even possible?

2 Answers 2

6

When you create the engine, you can add an "Arguments" option the contains your arguments:

IDictionary<string, object> options = new Dictionary<string, object>();
options["Arguments"] = new [] { "foo", "bar" };
_engine = Python.CreateEngine(options);

Now sys.argv will contain ["foo", "bar"].

You can set options["Arguments"] to anything that implements ICollection<string>.

Sign up to request clarification or add additional context in comments.

2 Comments

You didn't specify the version, but this might require 2.6 - I don't have 2.0 around to check.
Is there anything additional required on the python script side for this to work?
1

sys.argv is going to eventually be returning the actual parameters passed into the hosting app. Here's a few ways to solve this:

  1. Rewrite your script to accept Global Variables, then use ScriptScope.SetVariable
  2. Run the script, but rely on it to actually return you a class, which you can invoke methods on (much easier in 4.0 with dynamic keyword)
  3. Override sys.argv with your own implementation (this is a dynamic language after all!)

1 Comment

Hmmm... I think #3 will be the way to go. I suppose I will have to execute 'import sys' and 'sys.argv = args' in my ScriptScope as statements from C#? Then 'import sys' and 'args = sys.argv' in my script would work, yes? I have to do it this way in the script (import sys when sys exists) to get Sphinx to recognize my scripts as valid Python and create documentation.

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.