如何在.NET中导入第三方IronPython模块?

时间:2015-05-27 20:08:39

标签: c# python ironpython ironpython-studio

我是C#开发人员,我必须在.NET框架中使用IronPython库。我用Python测试了每个类,但是我不知道如何在C#类中调用库。

当我尝试调用库时,我收到'LightException' object has no attribute客户端错误。

我添加了lib,-x:全帧以及lib文件夹中的所有模块。

以下是我用来调用Python库的C#代码:

            Console.WriteLine("Press enter to execute the python script!");
            Console.ReadLine();
            var options = new Dictionary<string, object>();
            options["Frames"] = true;
            options["FullFrames"] = true;
            //var py = Python.CreateEngine(options);
                        //py.SetSearchPaths(paths);

            ScriptEngine engine = Python.CreateEngine(options);
            ICollection<string> paths = engine.GetSearchPaths();
            string dir = @"C:\Python27\Lib\";
            paths.Add(dir);
            string dir2 = @"C:\Python27\Lib\site-packages\";
            paths.Add(dir2);

            engine.SetSearchPaths(paths);
            ScriptSource source = engine.CreateScriptSourceFromFile(@"C:\Users\nikunjmange\Source\Workspaces\Visage Payroll\VisagePayrollSystem\VisagePayrollSystem\synapsepayLib\synapse_pay-python-master\synapse_pay\resources\user.py");
            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            dynamic Calculator = scope.GetVariable("User");
            dynamic calc = Calculator();

            string inputCreate = "nik12@gmail.com";
            string result = calc.create(inputCreate);

1 个答案:

答案 0 :(得分:2)

  1. 该错误具有误导性because of a bug in IronPython 2.7.5。它应该是ImportError

  2. 不要添加正常的CPython stdlib;它与IronPython不兼容。请改用IronPython的stdlib。

  3. 如果您导入的import a.b as c可能是罪魁祸首; a或b不存在,但IronPython破坏了错误报告。