将C#中的一个参数传递给Python
本文关键字:一个 参数传递 Python | 更新日期: 2023-09-27 18:21:52
我想使用ProcessStartInfo将C#代码中的一个参数传递给Python。我不得不避免使用IronPython(通过创建作用域),因为它与numpy不兼容。我找到了一些不成功的答案。这是我写的:
var x = 8;
string arg = string.Format(@"C:'Users'ayed'Desktop'IronPythonExamples'RunExternalScript'plot.py", x);
try
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo(@"D:'WinPython'WinPython-64bit-2.7.5.3'python-2.7.5.amd64'python.exe", arg);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("There is a problem in your Python code: " + ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
测试python代码允许绘制一条曲线并打印x的值。对于曲线来说,可以,但我得到了这个异常:没有定义名称"x"。
如何将变量正确地传递给这个python代码?
看起来x
应该是plot.py
的参数,但您忘记了将x
添加到arg
中。
string.Format(@"C:'Users'ayed'Desktop'IronPythonExamples'RunExternalScript'plot.py {0}", x);