1

我正在使用 IronPython 并让它以某种方式工作。不过,似乎没有将命令行参数传递给 Iron Python 的资源。如何从我的 C# 代码将命令行参数传递给 python 程序?

setup = new ScriptRuntimeSetup();
                setup.LanguageSetups.Add(IronPython.Hosting.Python.CreateLanguageSetup(null));
                runtime = new ScriptRuntime(setup);
                runtime.IO.RedirectToConsole();
                m_engine = runtime.GetEngine("IronPython");
                m_scope = m_engine.CreateScope();
                source = m_engine.CreateScriptSourceFromString("search_movie.py \""+CommandTextBox.Text+"\"", SourceCodeKind.InteractiveCode);
                source.Execute(m_scope);

我还需要在这里做什么?如您所见,python 文件的名称是 search_movie.py,它以电影名称作为参数。

4

2 回答 2

2

C#代码:

string file = @"C:\Users\M GHOUS\Source\Repos\dotnet_python\dotnet_python\test.py";
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
scope.SetVariable("var1", "data of var1");
scope.SetVariable("var2", "data of var2");
scope.SetVariable("var3", "data of var3");
engine.ExecuteFile(file, scope);

Python:

print(var1)
print(var2)
print(var3)
于 2019-01-15T17:46:51.770 回答
1

您需要设置 sys.argv 的值。

engine.Sys.argv = List.Make(args);

查看http://www.voidspace.org.uk/ironpython/custom_executable.shtml了解更多详情

于 2011-05-13T13:35:01.763 回答