使用命令行参数从C#运行Python脚本

时间:2022-09-03 07:12:43

I need to execute Python script from C# using IronPython and pass into it some command line parameters. Is there any way to do it?

我需要使用IronPython从C#执行Python脚本并将一些命令行参数传递给它。有什么办法吗?

My sample code:

我的示例代码:

        var e = Python.CreateEngine();
        var source = e.CreateScriptSourceFromFile(@"...");
        source.Execute();

1 个解决方案

#1


4  

Sure. When you create your engine, call the overload that takes in additional options and set the "Arguments" to your array of strings.

当然。创建引擎时,调用带有其他选项的重载并将“Arguments”设置为字符串数组。

var options = new Dictionary<string, object>
{
    { "Arguments", new[] { "foo", "bar", "biz baz" } },
};
var engine = Python.CreateEngine(options);
var script = @"
import sys
print sys.argv # ['foo', 'bar', 'biz baz']
";
engine.Execute(script);

#1


4  

Sure. When you create your engine, call the overload that takes in additional options and set the "Arguments" to your array of strings.

当然。创建引擎时,调用带有其他选项的重载并将“Arguments”设置为字符串数组。

var options = new Dictionary<string, object>
{
    { "Arguments", new[] { "foo", "bar", "biz baz" } },
};
var engine = Python.CreateEngine(options);
var script = @"
import sys
print sys.argv # ['foo', 'bar', 'biz baz']
";
engine.Execute(script);