c# - 从 C# 调用 Python 方法(没有 IronPython)

标签 c# python

<分区>

如何从我的 C# 方法调用 Python 脚本中的函数? (没有 IronPython)

在 hello.py 中

def hello_world(msg):
    print msg

在测试.cs中

        ProcessStartInfo start = new ProcessStartInfo ();
        start.FileName = "hello.py";
        start.Arguments = string.Format("{0}", arg);
        start.UseShellExecute = true;
        start.RedirectStandardOutput = true;

        using(Process process = Process.Start(start))
        {

        }

最佳答案

操作系统可能没有正确解析文件关联,因此它不知道如何执行 .py 文件。

相反,尝试将“python.exe”作为文件名并将 hello.py 作为参数。 exe 的确切名称当然取决于您安装的 python 版本。

举个例子:

    ProcessStartInfo start = new ProcessStartInfo ();
    start.FileName = @"c:\python\python27.exe";
    start.Arguments = string.Format("hello.py {0}", arg);
    start.UseShellExecute = true;
    start.RedirectStandardOutput = true;

    using(Process process = Process.Start(start))
    {

    }

(在上面的示例中,您当然必须替换 python 可执行文件的完整路径才能使其正常工作)

关于c# - 从 C# 调用 Python 方法(没有 IronPython),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30674504/

相关文章:

c# - XAML Hub 控制 Windows 8.1 通用应用程序

c# - ASP.Net Core LogLevel 不工作

c# - (更有可能)一个非常微妙的行为的迭代器错误?

javascript - python + selenium - 专注于无限滚动加载

python - ttk.Separator 设置长/宽

python - 在指定条件下对子列表中包含的值求和

python - 如何在python中从 `dictreader`中获取数据

c# - 在垃圾收集之前检测堆损坏

c# - 如果底层 DataContext 为空,如何隐藏控件?

python - 在 bash 脚本中使用反引号,将 python 脚本调用到变量中以前可以工作,但现在已损坏