首先需要引入IronPython,可以通过NuGet搜索获得,基于4.5以上框架集
using System; using IronPython.Hosting; using Microsoft.Scripting.Hosting; public partial class python : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { RunPythonShell(); } /// <summary> /// 调用Python /// </summary> private void RunPythonShell() { ScriptRuntime pyRuntime = Python.CreateRuntime(); //python文件绝对路径 string path = string.Format(@"{0}1.py", Server.MapPath("./")); dynamic py = pyRuntime.UseFile(path); //调用Python 的函数run() Response.Write(py.show()); } }
python文件代码 1.py
def show (): return "hello world!"