将参数从 C# 传递到 Python

Pass a parameter from C# to Python

我想使用 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);