调用从C#Python函数(.NET) [英] Call Python function from c# (.NET)
问题描述
我的Visual Studio 2015年我用C#编写的主要形式,并从那里我用Python编写的不同类别(普通的Python没有铁的Python)。我如何调用Python函数从我的C#代码?
I have Visual Studio 2015 with my main form written in C# and from there I have different classes written in Python (normal Python not Iron Python). How do I call the Python functions from my C# Code?
我知道有这个多个主题,但其中大部分是太旧了,有些解决方案是太困难或涉及使用中间语言如C ++。
I know there are multiple topics about this but most of them are too old and some solutions are too difficult or involve to use a middle language like C++.
下面有一些链接,我发现有用的,但并没有提供答案,我确切地寻找:
Here are some links I found useful but didn’t provided with the answer I was exactly searching for:
的 stackoverflow.com/questions/27075671/calling-python-from-net-via-c-bridge
有没有一种简单的方法或者说我还需要一个解决方法吗?如果我需要一个解决方法则是一个最简单的?
Is there an easy way or do I still need a workaround? And if I need a workaround then what is the easiest one?
推荐答案
您可以调用一切
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "python whatever";
process.StartInfo = startInfo;
process.Start();
甚至更好,只需拨打 Python.exe
并通过您的 PY
文件作为它的参数:
Or even better, just call Python.exe
and pass your py
files as its argument:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Python.exe";
startInfo.Arguments = "yourfile.py";
process.StartInfo = startInfo;
process.Start();
这篇关于调用从C#Python函数(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!