IronPython.Runtime.Exceptions.ImportException: No module named struct

Jun 28, 2011 at 10:43 AM

hello

Is there a workaround to solve this runtime error in C#.Net integrating IronPython?

Basically,i dont use the struct per se in the program.I just want to check if C# will compile together the "import struct" in my python file.

But during runtime, it gave me this runtime error.

In the IronPython console, I run manually the "import struct" and it did not give any problem..

Note: I used Microsoft Visual C# 2010 Express and IronPython2.7

Below is the runtime error for C#.

Unhandled Exception: IronPython.Runtime.Exceptions.ImportException: No module named struct
   at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
   at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path)

 

This is my code in python called calc.py

###################################

import struct
class Calculator:
   def add(self, argA, argB):
      return argA+argB
   def sub(self, argA, argB):
      return argA-argB

###################################

 

This is my code in C#.Net

###################################

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using System.IO;

namespace Calculator {
    class Program {
        static void Main(string[] args) {

            string path = Directory.GetCurrentDirectory();
            Console.WriteLine(path);

            var engine = Python.CreateEngine();
            dynamic py = engine.ExecuteFile(path + @"\calc.py");

            dynamic calc = py.Calculator();
            Console.WriteLine(calc.__class__.__name__);
            //write 'Calculator'

            double a = 7.5;
            double b = 2.5;
            double res;
            res = calc.add(a, b);
            Console.WriteLine("{0} + {1} = {2}", a, b, res);
            res = calc.sub(a, b);
            Console.WriteLine("{0} - {1} = {2}", a, b, res);
           
        }
    }
}

###################################