【问题标题】:How do I pass arguments to a Python script with IronPython如何使用 IronPython 将参数传递给 Python 脚本
【发布时间】:2015-05-21 21:36:26
【问题描述】:

我有以下 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);
        }
    }
}

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

【问题讨论】:

标签: c# python ironpython


【解决方案1】:

感谢大家为我指明正确的方向。由于某种原因,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】:

    “命令行参数”仅适用于进程。如果您以这种方式运行代码,您的 Python 脚本很可能会在启动时看到传递给您的进程的参数(没有 Python 代码,很难分辨)。 正如 cmets 中所建议的,您可以 override command line arguments too

    如果你想做的是传递参数,不一定是命令行参数,那么有几种方法。

    最简单的方法是将变量添加到您定义的范围并在脚本中读取这些变量。例如:

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

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

    【讨论】:

      【解决方案3】:

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

      因此,要回答这个问题,你应该使用engine.Sys.argv

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

      参考资料:

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

      How can I pass command-line arguments in IronPython?

      【讨论】:

      • 谢谢!我对engine.Sys 的一个问题是,在我的程序中,我收到一条消息,说明如下:“'Microsoft.Scripting.Hosting.ScriptEngine' 不包含'Sys' 的定义,并且没有扩展方法'Sys' 接受第一个可以找到“Microsoft.Scripting.Hosting.ScriptEngine”类型的参数(您是否缺少 using 指令或程序集引用?)”。如何让这个错误消失并让 engine.Sys 被识别?
      • 嗯.. 我认为该示例使用的是 PythonRuntime 实例。我需要多花点时间。
      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2012-12-29
      • 1970-01-01
      相关资源
      最近更新 更多