[英]Passing command line parameters to IronPython from C# application?
如何將命令行參數從我的C#應用程序傳遞到IronPython 2.x? Google僅返回有關如何使用Iron Python 1.x執行此操作的結果。
static void Main(string[] args)
{
ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();
// Pass in script file to execute but how to pass in other arguments in args?
ScriptScope scope = scriptRuntime.ExecuteFile(args[0]);
}
您可以通過以下C#代碼設置sys.argv:
static void Main(string[] args)
{
var scriptRuntime = Python.CreateRuntime();
var argv = new List();
args.ToList().ForEach(a => argv.Add(a));
scriptRuntime.GetSysModule().SetVariable("argv", argv);
scriptRuntime.ExecuteFile(args[0]);
}
有以下python腳本
import sys
for arg in sys.argv:
print arg
並調用exe之類的
Test.exe SomeScript.py foo bar
給你輸出
SomeScript.py
foo
bar
另一個選擇是將准備好的選項傳遞給Python.CreateRuntime
如本答案中所述
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.