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

本文关键字:第三方 IronPython 模块 导入 net | 更新日期: 2023-09-27 17:50:52

我是c#开发人员,我必须在。net框架中使用IronPython库。我测试了Python中的每个类,它都在工作,但我不确定如何在c#类中调用库。

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

我已经添加了lib, -x:Full frame和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);

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

  1. 这个错误是误导性的,因为IronPython 2.7.5中的一个错误。应该是ImportError

  2. 不要添加普通的CPython stdlib;它与IronPython不兼容。使用IronPython的stdlib代替。

如果你进口了import a.b as c,那可能就是罪魁祸首;a或b都不存在,但是IronPython混淆了错误报告。