如何使用IronPython将参数传递给Python脚本(How do I pass arguments to a Python script with IronPython)

编程入门 行业动态 更新时间:2024-10-14 18:21:03
如何使用IronPython将参数传递给Python脚本(How do I pass arguments to a Python script with IronPython)

我有以下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脚本?

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

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?

最满意答案

谢谢大家指点我正确的方向。 出于某种原因,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); } } }

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:

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); } } }

更多推荐

本文发布于:2023-08-06 20:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   脚本   参数   IronPython   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!