nikunjM
nikunjM

Reputation: 590

How do I import a third-party IronPython module in .NET?

I am C# developer and I have to use an IronPython library in the .NET framework. I tested every class in Python and it's working but I am not sure how to call the library in a C# class.

When I try to call the library, I am getting a 'LightException' object has no attribute client error.

I have added lib, -x:Full frame and also all modules in the lib folder.

Here is the C# code I am using to call the Python library:

            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 = "[email protected]";
            string result = calc.create(inputCreate);

Upvotes: 1

Views: 2109

Answers (1)

Jeff Hardy
Jeff Hardy

Reputation: 7662

  1. The error is misleading because of a bug in IronPython 2.7.5. It should be an ImportError.

  2. Don't add the normal CPython stdlib; it's not compatible with IronPython. Use IronPython's stdlib instead.

If you have an import of import a.b as c that's probably the culprit; either a or b does not exist but IronPython mucks up the error reporting.

Upvotes: 2

Related Questions