需要在Nuget安装IronPython,
如果报错,需要更新Nuget版本则下载对应版本即可:https://dist.nuget.org/index.html
添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安装目录中)
c#文件: using IronPython.Hosting; using Microsoft.Scripting.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PythonDemo { class Program { static void Main(string[] args) { ScriptRuntime pyRunTime = Python.CreateRuntime(); dynamic obj = pyRunTime.UseFile("hello.py"); Console.WriteLine(obj.welcome("Nick")); Console.WriteLine(obj.add(1,3)); Console.ReadKey(); } } } python文件: #hello.py def welcome(name): return "hello " + name def add(a,b): return a+b
运行结果: