如何使用IronPython将参数传递给Python脚本

时间:2021-12-13 07:13:34

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

我有以下C#代码,我从C#调用python脚本:

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?

我无法理解代码的每一行,因为我对C#的经验是有限的。当我运行它时,如何更改此代码以将命令行参数传递给我的python脚本?

3 个解决方案

#1


5  

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:

谢谢大家指出我正确的方向。出于某种原因,engine.sys似乎不再适用于更新版本的IronPython,因此必须使用GetSysModule。这是我的代码的修订版本,允许我更改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


3  

"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.

“命令行参数”仅存在于进程中。如果你以这种方式运行代码,你的python脚本很可能会在启动时看到传递给你的进程的参数(没有Python代码,很难说)。正如评论中所建议的那样,您也可以覆盖命令行参数。

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.

在python代码中,您将拥有variableName变量。

#3


2  

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

虽然我认为@ Discord使用设置变量会起作用,但需要将sys.argvs更改为variableName。

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

因此,要回答这个问题,你应该使用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

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

How can I pass command-line arguments in IronPython?

如何在IronPython中传递命令行参数?

#1


5  

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:

谢谢大家指出我正确的方向。出于某种原因,engine.sys似乎不再适用于更新版本的IronPython,因此必须使用GetSysModule。这是我的代码的修订版本,允许我更改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


3  

"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.

“命令行参数”仅存在于进程中。如果你以这种方式运行代码,你的python脚本很可能会在启动时看到传递给你的进程的参数(没有Python代码,很难说)。正如评论中所建议的那样,您也可以覆盖命令行参数。

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.

在python代码中,您将拥有variableName变量。

#3


2  

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

虽然我认为@ Discord使用设置变量会起作用,但需要将sys.argvs更改为variableName。

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

因此,要回答这个问题,你应该使用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

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

How can I pass command-line arguments in IronPython?

如何在IronPython中传递命令行参数?