c# - 将参数从 C# 传递到 Python

标签 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的值。曲线没问题,但我收到此异常:名称“x”未定义。

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

最佳答案

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

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

关于c# - 将参数从 C# 传递到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694104/

相关文章:

c# - 查找包含特定单词的特定属性

python - 类型错误 : unbound method must be called with instance as first argument (got int instance instead) in Python 2

ironpython - 如何将 IronPython 与 App.Config 一起使用?

.net - 在 ironpython 中开始 .net 开发

javascript - 如何在 Spotfire 仪表板负载上运行 IronPython 脚本

C# 使用强制转换将 Int64 转换为字符串

c# - 带有可见可点击控件的透明表单

c# - NewThreadScheduler.Default 安排同一个线程上的所有工作

python - 创建列列表并使用 Pandas (Python) 将它们汇总到一个新列中

Python Django Time Zone Conversion Incorrect Time for 'US/Pacific' 时区