将参数从C#传递给Python

时间:2015-11-13 13:42:03

标签: c# python ironpython

我想使用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的值。好的曲线,但我得到了这个例外:name' x'没有定义。

如何正确地将变量传递给此python代码?

1 个答案:

答案 0 :(得分:1)

看起来x应该是plot.py的参数,但您忘记将x添加到arg

string.Format(@"C:\Users\ayed\Desktop\IronPythonExamples\RunExternalScript\plot.py {0}", x);
相关问题