//在NuGet中安装pythonnet插件
using Python.Runtime;
private void bt_python_Click(object sender, EventArgs e)
{
dynamic dynamic;
Stopwatch sw = new Stopwatch();
sw.Start();
Runtime.PythonDLL = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"C:\Programs\Python\Python312-32\python312.dll");//编译python程序的工具路径
//引用python引擎
PythonEngine.Initialize();
using (Py.GIL())
{
//导入python脚本
dynamic = Py.Import("240821");//240821.py
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds.ToString());//用时单位:毫秒
string result = dynamic.hello("888");
Console.WriteLine(result);
PythonEngine.Shutdown();
}
//240821.py内容:
def hello (name):
return f"Hello,{name}!"