首先需要引入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!"
