There are four common methods for C# to call Python. The reason for choosing this is that it is relatively simple, has a low error rate and a wide range of
applications. Those who are familiar with Python development know that third-party libraries are inevitable when writing Python programs. Here It is also the most error-prone place to call python programs.
Of course .exe
, the way it also causes the greatest performance loss.
- Take the terminal program as an example below
Core code
using System.Diagnostics;
namespace 关于CSharp调用python
{
class Program
{
static void Main(string[] args)
{
Process process = new Process();
// 将 python exe 程序放在 debug 文件下 直接调用即可
process.StartInfo.FileName = @"XXX.exe";
//必需
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
//关闭程序,等待外部程序退出后才能往下执行
process.WaitForExit();
process.Close();
}
}
}
ps:Understand the principle