How do I pass arguments to a Python script with IronPython

断了今生、忘了曾经 提交于 2019-12-04 03:16:43

问题


I have the following C# code where I call a python script from C#:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;

namespace RunPython
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
            ScriptRuntime runtime = new ScriptRuntime(setup);
            ScriptEngine engine = Python.GetEngine(runtime);
            ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);
        }
    }
}

I'm having trouble understanding each line of the code because my experience with C# is limited. How would I alter this code in order to pass a command line argument to my python script when I run it?


回答1:


Thank you all for pointing me in the correct direction. For some reason engine.sys seems to no longer work for more recent versions of IronPython so instead GetSysModule must be used. Here is the revised version of my code that allows me to alter argv:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython.Runtime;

namespace RunPython
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
            ScriptRuntime runtime = new ScriptRuntime(setup);
            ScriptEngine engine = Python.GetEngine(runtime);
            ScriptSource source = engine.CreateScriptSourceFromFile("HelloWorld.py");
            ScriptScope scope = engine.CreateScope();
            List<String> argv = new List<String>();
            //Do some stuff and fill argv
            argv.Add("foo");
            argv.Add("bar");
            engine.GetSysModule().SetVariable("argv", argv);
            source.Execute(scope);
        }
    }
}



回答2:


"Command line arguments" exists only for processes. If you run code this way, you python script most likely will see arguments passed to your process when it was started (without Python code, it's hard to tell). As suggested in the comments, you can override command line arguments too.

If what you want to do is pass arguments, not necessarily command line arguments, then there're several approaches.

The most easy would be to add variables to the scope you've defined and read these variables in the script. For example:

int variableName = 1337;
scope.SetVariable("variableName", variableName);

In the python code, you'll have variableName variable.




回答3:


While I think that @Discord's use of setting a variable would work, it would require some changing of sys.argvs to variableName.

Therefore, to answer the question, you should use engine.Sys.argv:

List<int> argv = new List<int>();
//Do some stuff and fill argv
engine.Sys.argv=argv;

References:

http://www.voidspace.org.uk/ironpython/custom_executable.shtml

How can I pass command-line arguments in IronPython?



来源:https://stackoverflow.com/questions/30384793/how-do-i-pass-arguments-to-a-python-script-with-ironpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!