如何使用IronPython引擎在C#中导入python模块?

时间:2019-04-26 06:33:26

标签: c# python ironpython

这是我的C#代码,用于加载python函数。

SELECT AVG(dd.employee_id) as Median_Val
FROM (
    SELECT 
    d.employee_id, 
    @rownum:=@rownum+1 as `row_number`, 
    @total_rows:=@rownum
    FROM pasmt_employee d 
    join (SELECT @rownum:=0) r
    ORDER BY d.employee_id
      ) as dd
WHERE dd.row_number IN ( FLOOR((@total_rows+1)/2), FLOOR((@total_rows+2)/2) ) ;

这是我的“ simple.py”代码。

 static void Main(string[] args)
    {
        var engine = IronPython.Hosting.Python.CreateEngine();

        var scope = engine.CreateScope();

        try
        {
            var source = engine.CreateScriptSourceFromFile("simple.py");
            source.Execute(scope);

            var Fnnumpy = scope.GetVariable<Func<object>>("numpytest");
            Console.WriteLine(Fnnumpy());
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

当我运行C#代码时,会弹出此错误消息

import numpy as np

def numpytest():
   x = np.random.rand()
   return x

任何其他没有'import'的python函数在C#中都可以正常工作。仅那些具有导入功能会引起问题。 看来我的C#代码不知道numpy模块在哪里,但我不知道如何纠正它。如何在C#中导入python库?请帮助我。

0 个答案:

没有答案