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

 

相关文章:

  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2021-04-23
  • 2021-04-05
  • 2021-10-24
  • 2022-12-23
猜你喜欢
  • 2021-05-25
  • 2021-11-18
  • 2022-02-24
  • 2021-10-27
  • 2021-12-28
  • 2022-12-23
  • 2021-06-06
相关资源
相似解决方案