需要在Nuget安装IronPython,
如果报错,需要更新Nuget版本则下载对应版本即可:https://dist.nuget.org/index.html

添加引用:IronPython.dll,Microsoft.Scripting.dll(在IronPython的安装目录中)

c#文件:
  1. using IronPython.Hosting;
  2. using Microsoft.Scripting.Hosting;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace PythonDemo
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             ScriptRuntime pyRunTime = Python.CreateRuntime();
  15.             dynamic obj = pyRunTime.UseFile("hello.py");
  16.  
  17.             Console.WriteLine(obj.welcome("Nick"));
  18.             Console.WriteLine(obj.add(1,3));
  19.             Console.ReadKey();
  20.         }
  21.     }
  22. }

python文件:
  1. #hello.py
  2. def welcome(name):
  3.     return "hello " + name
  4.  
  5. def add(a,b):
  6.     return a+b

运行结果:
c#调用python脚本函数_Python编程教学