gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:04:38 28 4
gpt4 key购买 nike

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

我的示例代码:

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

最佳答案

当然。当您创建引擎时,调用接受额外选项的重载并将 "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);

关于c# - 使用命令行参数从 C# 运行 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12243180/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com