加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中执行python脚本

发布时间:2020-12-15 22:22:38 所属栏目:百科 来源:网络整理
导读:我试图在C#中执行 python代码.通常它应该使用Iron Python并在安装PTVS之后完成(我正在使用VS 2010). var pyEngine = Python.CreateEngine(); var pyScope = pyEngine.CreateScope(); try { pyEngine.ExecuteFile("plot.py",pyScope); } catch (Exception ex)
我试图在C#中执行 python代码.通常它应该使用Iron Python并在安装PTVS之后完成(我正在使用VS 2010).

var pyEngine = Python.CreateEngine();  
        var pyScope = pyEngine.CreateScope();   

        try
        {
           pyEngine.ExecuteFile("plot.py",pyScope);

        }
        catch (Exception ex)
        {
            Console.WriteLine("There is a problem in your Python code: " + ex.Message);
        }

问题是IronPython似乎无法识别像numpy,pylab或matplotlib这样的库.我看了一下,发现有些人在谈论Enthought Canopy或Anaconda,我已经安装了两个而没有解决问题.
我该怎么做才能解决问题?

解决方法

为了执行导入一些库(如numpy和pylab)的Python脚本,可以这样做:

string arg = string.Format(@"C:UsersayedDesktopIronPythonExamplesRunExternalScriptplot.py"); // Path to the Python code
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo(@"D:WinPythonWinPython-64bit-2.7.5.3python-2.7.5.amd64python.exe",arg);
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true; // Hide the command line window
    p.StartInfo.RedirectStandardOutput = false;
    p.StartInfo.RedirectStandardError = false;
    Process processChild = Process.Start(p.StartInfo);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读