1

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 = "nik12@gmail.com";
            string result = calc.create(inputCreate);
Air
  • 8,274
  • 2
  • 53
  • 88
nikunjM
  • 560
  • 1
  • 7
  • 21

1 Answers1

2
  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.

Jeff Hardy
  • 7,632
  • 24
  • 24
  • Can you please explain in detail or give example.. That will help me to understand. @Jeff Hardy – nikunjM May 28 '15 at 18:48
  • It depends on how are referencing IronPython. If it's from a NuGet package, then you also need to include the IronPython.StdLib package. If you assume the end user has IronPython installed then `C:\Program Files\IronPython 2.7\Lib` instead. – Jeff Hardy May 29 '15 at 14:46
  • Then check the imports in your user.py script to see if any are of the form 'import a.b as c'. If either a or b cannot be found you'll get this error. – Jeff Hardy May 29 '15 at 14:48
  • Client is local file and I think when your config python with c# its big headache... I think writing c# lib is better and faster way.. Becoz when you upload it to server,even then you will face lot of problem. So i started with c# and its almost done. But thanks for suggestion, and hopes iron python will improve and also its documentation. – nikunjM May 29 '15 at 19:41